How to check email address already exist in MongoDB database ? RRS feed

  • Question

  • User-150677302 posted

    I am trying to make a user registration page, when a user is trying to register his/her email id, it should show email already exists ?
    How to do this in Mongodb ??

    Wednesday, December 23, 2020 11:59 AM

All replies

  • User-474980206 posted

    Start with the docs


    Wednesday, December 23, 2020 3:26 PM
  • User-150677302 posted

    Thanks Bruce, but I Had already gone through that, didn't help out that much in my problem !

    Thursday, December 24, 2020 7:15 AM
  • User1686398519 posted

    Hi aadi765432, 

    According to your needs, I wrote an example, you can refer to it.

    1. In this example, I did not connect to the database, just made a simple judgment. You need to check if the email exists in the database.
    2. You can use Ajax.BeginForm and set OnBegin to check whether the email exists before the form is submitted. If the email exists, the form will not be submitted and an error message will be given.
      • You need to use NuGet to install Microsoft.jQuery.Unobtrusive.Ajax and reference the jquery.unobtrusive-ajax.min.js file.


        public class TestEmailController : Controller
            // GET: TestEmail
            public ActionResult Index()
                return View();
            public ActionResult Test(string email)
                return RedirectToAction("Index");
            public ActionResult Checkemial(string email)
                var result = false;
                //Here for database (MongoDB) query
                if (email == "test@test.com")
                    result = true;
                return Json(result,JsonRequestBehavior.AllowGet);


        ViewBag.Title = "Index";
        Layout = null;
    @using (Ajax.BeginForm("Test", "TestEmail", FormMethod.Post,
            new AjaxOptions
                OnBegin = "return Validate();"
        <input name="email" /><span id="message"></span>
        <button type="submit">submit</button>
    <script src="~/Scripts/jquery-3.4.1.min.js"></script>
    <script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
            function Validate() {
                if (!checkemail()) {
                    $("#message").text("success");return true;
                } else {
                    $("#message").text("email has exsist!");return false;
            function checkemail() {
                var flag = false;
                 var emailval = $("input[name='email']").val();
                    type: "GET",
                    url: "@Url.Action("Checkemial")",
                    data: { email: emailval },
                    async: false,
                    success: function (result) {
                        flag = result;
                return flag;

    Here is the result. 

    Best Regards,


    Thursday, December 24, 2020 9:16 AM
  • User753101303 posted


    Please avoid to post the same question in multiple threads. To get a better response, the point is to be accurate about the problem you have.

    From the other thread it seems the current show stopper is learning about to write queries against your MongoDB database? I gave sdome pointers about that in your other thead.

    Thursday, December 24, 2020 9:42 AM
  • User-474980206 posted

    Thanks Bruce, but I Had already gone through that, didn't help out that much in my problem !

    what part do you not understand? 

    • building the form
    • writing ab action
    • query mongo db
    • display and error

    to hep with the query we would need the database name, collection name and schema. but it owl be something like


    var lookupEmail = "some@foo.com";
    var client = new MongoClient(connectString);
    var database = client.GetDatabase("mydatabase");
    var collection = database.GetCollection<BsonDocument>("mycollection");
    var builder = Builders<BsonDocument>.Filter;
    var filter = builder.Eq("email", lookupEmail);
    var result = collection.Find(filter).ToList();
    var emailFound = result.Count > 0;

    Thursday, December 24, 2020 4:25 PM
  • User-150677302 posted

    Thanks so much for this.!

    can you review my code.  i tried to do this in Lines  48-52 && 131- 136

    Friday, December 25, 2020 2:13 PM
  • User-150677302 posted

    query mongo db

    Having Error in Querying.
    I tried to do in lines 48 to 52 and 131 to 136, Here the link to my code

    Friday, December 25, 2020 2:16 PM
  • User753101303 posted

    Missing a using System.Linq; at the top maybe?

    Always tell which error you have so that we can move directly at fixing the error rather than having first to guess which error you see.

    Friday, December 25, 2020 7:13 PM