Reputation: 23
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? Got any ideas? Here is my code so far
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");
}
Upvotes: 1
Views: 3294
Reputation: 14287
This works fine to get the count of documents after the filter is applied:
var crsr = collection.Find(filter);
var size = crsr.CountDocuments();
Console.WriteLine(size); // prints the document count
Upvotes: 2