[英]How do I count documents with a filter in mongodb from c#
I am trying to make this email validation thing with c# and mongodb, but i don't know how am i suppose to count the documents with a filter in mongoDB? 有什么想法吗? 到目前为止,这是我的代码
public ActionResult Saveuserinput(string firstname, string lastname, string dob, string studentid, string gender, string email, string phonenumber, string address, string city, string zipcode, string state,string password)
{
ViewData["message"] = $"Account created";
var client = new MongoClient("connection string");
var database = client.GetDatabase("AccountInfo");
var collec = database.GetCollection<BsonDocument>("AccountInfo");
var filter = Builders<BsonDocument>.Filter.Eq("Email", email);
var Userinfo = collec.Find(filter).First();
int avalibility = collec.CountAsync(filter);
if (avalibility > 1)
{
return Content("Email Taken");
}
else
{
var document = new BsonDocument
{
{"Firstname", firstname },
{"Lastname", lastname },
{"Date of birth", dob },
{"Student ID", studentid },
{"Gender", gender },
{"Email", email },
{"Password", password },
{"Phone number", phonenumber },
{"Address", address },
{"City", city },
{"Zipcode", zipcode },
{"State", state }
};
collec.InsertOneAsync(document);
return View("~/Views/SignIn/Index.cshtml");
}
在应用过滤器后,这可以很好地获取文档数:
var crsr = collection.Find(filter);
var size = crsr.CountDocuments();
Console.WriteLine(size); // prints the document count
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.