Can't find does not contain element in array in mongodb?










1















I am a beginner in mongodb. I want to check if a particular element is present in array. If the given element is already present in the array it should show already exists. If the given element is not present in array it should show this array doesn't contain this element. But my program it shows already exist for all input. What is the problem in my code?






var mongodb=require("mongodb");
var mongoclient=mongodb.MongoClient;
var url="mongodb://localhost:27017/hello";
mongoclient.connect(url,function(err,db)

if(err)

console.log(err);

else

console.log("connect the details database successfully");
var collection=db.collection("users");
collection.find("email":$contains:"sample@gmail.com",function(error,ok)

if(error)

console.log(error);

else if(ok)

console.log("already exist");

// print something if element is not present in array how can i check?
);
db.close();

);












share|improve this question




























    1















    I am a beginner in mongodb. I want to check if a particular element is present in array. If the given element is already present in the array it should show already exists. If the given element is not present in array it should show this array doesn't contain this element. But my program it shows already exist for all input. What is the problem in my code?






    var mongodb=require("mongodb");
    var mongoclient=mongodb.MongoClient;
    var url="mongodb://localhost:27017/hello";
    mongoclient.connect(url,function(err,db)

    if(err)

    console.log(err);

    else

    console.log("connect the details database successfully");
    var collection=db.collection("users");
    collection.find("email":$contains:"sample@gmail.com",function(error,ok)

    if(error)

    console.log(error);

    else if(ok)

    console.log("already exist");

    // print something if element is not present in array how can i check?
    );
    db.close();

    );












    share|improve this question


























      1












      1








      1








      I am a beginner in mongodb. I want to check if a particular element is present in array. If the given element is already present in the array it should show already exists. If the given element is not present in array it should show this array doesn't contain this element. But my program it shows already exist for all input. What is the problem in my code?






      var mongodb=require("mongodb");
      var mongoclient=mongodb.MongoClient;
      var url="mongodb://localhost:27017/hello";
      mongoclient.connect(url,function(err,db)

      if(err)

      console.log(err);

      else

      console.log("connect the details database successfully");
      var collection=db.collection("users");
      collection.find("email":$contains:"sample@gmail.com",function(error,ok)

      if(error)

      console.log(error);

      else if(ok)

      console.log("already exist");

      // print something if element is not present in array how can i check?
      );
      db.close();

      );












      share|improve this question
















      I am a beginner in mongodb. I want to check if a particular element is present in array. If the given element is already present in the array it should show already exists. If the given element is not present in array it should show this array doesn't contain this element. But my program it shows already exist for all input. What is the problem in my code?






      var mongodb=require("mongodb");
      var mongoclient=mongodb.MongoClient;
      var url="mongodb://localhost:27017/hello";
      mongoclient.connect(url,function(err,db)

      if(err)

      console.log(err);

      else

      console.log("connect the details database successfully");
      var collection=db.collection("users");
      collection.find("email":$contains:"sample@gmail.com",function(error,ok)

      if(error)

      console.log(error);

      else if(ok)

      console.log("already exist");

      // print something if element is not present in array how can i check?
      );
      db.close();

      );








      var mongodb=require("mongodb");
      var mongoclient=mongodb.MongoClient;
      var url="mongodb://localhost:27017/hello";
      mongoclient.connect(url,function(err,db)

      if(err)

      console.log(err);

      else

      console.log("connect the details database successfully");
      var collection=db.collection("users");
      collection.find("email":$contains:"sample@gmail.com",function(error,ok)

      if(error)

      console.log(error);

      else if(ok)

      console.log("already exist");

      // print something if element is not present in array how can i check?
      );
      db.close();

      );





      var mongodb=require("mongodb");
      var mongoclient=mongodb.MongoClient;
      var url="mongodb://localhost:27017/hello";
      mongoclient.connect(url,function(err,db)

      if(err)

      console.log(err);

      else

      console.log("connect the details database successfully");
      var collection=db.collection("users");
      collection.find("email":$contains:"sample@gmail.com",function(error,ok)

      if(error)

      console.log(error);

      else if(ok)

      console.log("already exist");

      // print something if element is not present in array how can i check?
      );
      db.close();

      );






      arrays mongodb






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 14:42









      guaka

      10.3k74481




      10.3k74481










      asked Oct 25 '17 at 17:53









      sampleprogrammerssampleprogrammers

      2718




      2718






















          1 Answer
          1






          active

          oldest

          votes


















          0














          Check the documentation:




          Anywhere a callback is passed to a query in Mongoose, the callback
          follows the pattern callback(error, results). What results is depends
          on the operation: For findOne() it is a potentially-null single
          document, find() a list of documents, count() the number of documents,
          update() the number of documents affected, etc. The API docs for
          Models provide more detail on what is passed to the callbacks.




          So, you will always get a list of documents which, however, can be empty.






          share|improve this answer






















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f46939025%2fcant-find-does-not-contain-element-in-array-in-mongodb%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Check the documentation:




            Anywhere a callback is passed to a query in Mongoose, the callback
            follows the pattern callback(error, results). What results is depends
            on the operation: For findOne() it is a potentially-null single
            document, find() a list of documents, count() the number of documents,
            update() the number of documents affected, etc. The API docs for
            Models provide more detail on what is passed to the callbacks.




            So, you will always get a list of documents which, however, can be empty.






            share|improve this answer



























              0














              Check the documentation:




              Anywhere a callback is passed to a query in Mongoose, the callback
              follows the pattern callback(error, results). What results is depends
              on the operation: For findOne() it is a potentially-null single
              document, find() a list of documents, count() the number of documents,
              update() the number of documents affected, etc. The API docs for
              Models provide more detail on what is passed to the callbacks.




              So, you will always get a list of documents which, however, can be empty.






              share|improve this answer

























                0












                0








                0







                Check the documentation:




                Anywhere a callback is passed to a query in Mongoose, the callback
                follows the pattern callback(error, results). What results is depends
                on the operation: For findOne() it is a potentially-null single
                document, find() a list of documents, count() the number of documents,
                update() the number of documents affected, etc. The API docs for
                Models provide more detail on what is passed to the callbacks.




                So, you will always get a list of documents which, however, can be empty.






                share|improve this answer













                Check the documentation:




                Anywhere a callback is passed to a query in Mongoose, the callback
                follows the pattern callback(error, results). What results is depends
                on the operation: For findOne() it is a potentially-null single
                document, find() a list of documents, count() the number of documents,
                update() the number of documents affected, etc. The API docs for
                Models provide more detail on what is passed to the callbacks.




                So, you will always get a list of documents which, however, can be empty.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Oct 25 '17 at 17:57









                dnicklessdnickless

                6,6451320




                6,6451320



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f46939025%2fcant-find-does-not-contain-element-in-array-in-mongodb%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    27

                    Top Tejano songwriter Luis Silva dead of heart attack at 64

                    Category:Rhetoric