How do you query for “is not null” in Mongo?












287















I would like to execute a following query:



db.mycollection.find(HAS IMAGE URL)


What should be the correct syntax?










share|improve this question




















  • 43





    Short answer: the query { field : {$ne : null} } check for not-null docs.mongodb.org/manual/reference/operator/query/ne

    – Jaider
    Jul 8 '15 at 13:31


















287















I would like to execute a following query:



db.mycollection.find(HAS IMAGE URL)


What should be the correct syntax?










share|improve this question




















  • 43





    Short answer: the query { field : {$ne : null} } check for not-null docs.mongodb.org/manual/reference/operator/query/ne

    – Jaider
    Jul 8 '15 at 13:31
















287












287








287


57






I would like to execute a following query:



db.mycollection.find(HAS IMAGE URL)


What should be the correct syntax?










share|improve this question
















I would like to execute a following query:



db.mycollection.find(HAS IMAGE URL)


What should be the correct syntax?







mongodb database nosql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 14:55









darijan

9,0172036




9,0172036










asked Oct 30 '10 at 4:04









TIMEXTIMEX

70.2k275651960




70.2k275651960








  • 43





    Short answer: the query { field : {$ne : null} } check for not-null docs.mongodb.org/manual/reference/operator/query/ne

    – Jaider
    Jul 8 '15 at 13:31
















  • 43





    Short answer: the query { field : {$ne : null} } check for not-null docs.mongodb.org/manual/reference/operator/query/ne

    – Jaider
    Jul 8 '15 at 13:31










43




43





Short answer: the query { field : {$ne : null} } check for not-null docs.mongodb.org/manual/reference/operator/query/ne

– Jaider
Jul 8 '15 at 13:31







Short answer: the query { field : {$ne : null} } check for not-null docs.mongodb.org/manual/reference/operator/query/ne

– Jaider
Jul 8 '15 at 13:31














8 Answers
8






active

oldest

votes


















651














This will return all documents with a key called "IMAGE URL", but they may still have a null value.



db.mycollection.find({"IMAGE URL":{$exists:true}});


This will return all documents with both a key called "IMAGE URL" and a non-null value.



db.mycollection.find({"IMAGE URL":{$ne:null}});


Also, according to the docs, $exists currently can't use an index, but $ne can.



Edit: Adding some examples due to interest in this answer



Given these inserts:



db.test.insert({"num":1, "check":"check value"});
db.test.insert({"num":2, "check":null});
db.test.insert({"num":3});


This will return all three documents:



db.test.find();


This will return the first and second documents only:



db.test.find({"check":{$exists:true}});


This will return the first document only:



db.test.find({"check":{$ne:null}});


This will return the second and third documents only:



db.test.find({"check":null})





share|improve this answer





















  • 6





    It's the correct answer!

    – redice
    Dec 6 '13 at 6:54






  • 11





    According to docs, $ne includes documents that do not contain the field. Has this changed since you posted the answer? docs.mongodb.org/manual/reference/operator/query/ne

    – Andrew Mao
    Sep 24 '14 at 4:40






  • 2





    I don't believe that has changed. When checking $ne, the value is checked in all documents, including those that don't contain the field, but $ne:null still will not match a document that does not contain the field since the value of the field is still null, even though the field doesn't exist in that document.

    – Tim Gautier
    Sep 24 '14 at 17:39






  • 2





    How do you just match the second document?

    – B T
    Apr 21 '16 at 7:34






  • 1





    @River I checked when I wrote this 3 years ago and, just to be sure, I just installed Mongo and tried it out again. It still works the same way, the answer is correct. The 2nd to last query returns only the 1st document.

    – Tim Gautier
    Nov 8 '17 at 16:54



















37














In pymongo you can use:



db.mycollection.find({"IMAGE URL":{"$ne":None}});


Because pymongo represents mongo "null" as python "None".






share|improve this answer































    23














    One liner is the best :



    db.mycollection.find({ 'fieldname' : { $exists: true, $ne: null } });


    Here,



    mycollection : place your desired collection name



    fieldname : place your desired field name



    Explaination :



    $exists : When is true, $exists matches the documents that contain the field, including documents where the field value is null. If is false, the query returns only the documents that do not contain the field.



    $ne selects the documents where the value of the field is not equal to the specified value. This includes documents that do not contain the field.



    So in your provided case following query going to return all the documents with imageurl field exists and having not null value:



    db.mycollection.find({ 'imageurl' : { $exists: true, $ne: null } });





    share|improve this answer



















    • 2





      $exists: true is redundant, $ne: null is enough.

      – Stanislav Karakhanov
      Nov 21 '18 at 20:13



















    13














    db.collection_name.find({"filed_name":{$exists:true}});



    fetch documents that contain this filed_name even it is null.



    My proposition:



    db.collection_name.find({"field_name":{$type:2}}) //type:2 == String


    You can check on the required attribute's type, it will return all the documents that its field_name queried contains a value because you are checking on the filed's type else if it is null the type condition doesn't match so nothing will be returned.



    N.b:if the field_name has an empty string which means "", it will be returned.It is the same behavior for
    db.collection_name.find({"filed_name":{$ne:null}});



    Extra validation:



    Okay, so we are not finished yet we need an extra condition.



    db.collection_name.
    find({ "field_name":{$type:2},$where:"this.field_name.length >0"})



    OR



    db.collection_name.
    find({ "field_name":{$ne:null},$where:"this.field_name.length >0"})



    Reference for all the types:
    https://docs.mongodb.com/manual/reference/operator/query/type/#op._S_type






    share|improve this answer

































      11















      Sharing for future readers.




      This query worked for us (query executed from MongoDB compass):



      {
      "fieldName": {
      "$nin": [
      "",
      null
      ]
      }
      }





      share|improve this answer





















      • 1





        { $exists: true, $ne: null } didn't show correct result. Your query works good

        – Oleksandr Buchek
        Aug 8 '18 at 15:59











      • @OleksandrBuchek Glad to know this man!

        – student
        Aug 9 '18 at 12:01



















      2














      An alternative that has not been mentioned, but that may be a more efficient option for some (won't work with NULL entries) is to use a sparse index (entries in the index only exist when there is something in the field). Here is a sample data set:



      db.foo.find()
      { "_id" : ObjectId("544540b31b5cf91c4893eb94"), "imageUrl" : "http://example.com/foo.jpg" }
      { "_id" : ObjectId("544540ba1b5cf91c4893eb95"), "imageUrl" : "http://example.com/bar.jpg" }
      { "_id" : ObjectId("544540c51b5cf91c4893eb96"), "imageUrl" : "http://example.com/foo.png" }
      { "_id" : ObjectId("544540c91b5cf91c4893eb97"), "imageUrl" : "http://example.com/bar.png" }
      { "_id" : ObjectId("544540ed1b5cf91c4893eb98"), "otherField" : 1 }
      { "_id" : ObjectId("544540f11b5cf91c4893eb99"), "otherField" : 2 }


      Now, create the sparse index on imageUrl field:



      db.foo.ensureIndex( { "imageUrl": 1 }, { sparse: true } )
      {
      "createdCollectionAutomatically" : false,
      "numIndexesBefore" : 1,
      "numIndexesAfter" : 2,
      "ok" : 1
      }


      Now, there is always a chance (and in particular with a small data set like my sample) that rather than using an index, MongoDB will use a table scan, even for a potential covered index query. As it turns out that gives me an easy way to illustrate the difference here:



      db.foo.find({}, {_id : 0, imageUrl : 1})
      { "imageUrl" : "http://example.com/foo.jpg" }
      { "imageUrl" : "http://example.com/bar.jpg" }
      { "imageUrl" : "http://example.com/foo.png" }
      { "imageUrl" : "http://example.com/bar.png" }
      { }
      { }


      OK, so the extra documents with no imageUrl are being returned, just empty, not what we wanted. Just to confirm why, do an explain:



      db.foo.find({}, {_id : 0, imageUrl : 1}).explain()
      {
      "cursor" : "BasicCursor",
      "isMultiKey" : false,
      "n" : 6,
      "nscannedObjects" : 6,
      "nscanned" : 6,
      "nscannedObjectsAllPlans" : 6,
      "nscannedAllPlans" : 6,
      "scanAndOrder" : false,
      "indexOnly" : false,
      "nYields" : 0,
      "nChunkSkips" : 0,
      "millis" : 0,
      "server" : "localhost:31100",
      "filterSet" : false
      }


      So, yes, a BasicCursor equals a table scan, it did not use the index. Let's force the query to use our sparse index with a hint():



      db.foo.find({}, {_id : 0, imageUrl : 1}).hint({imageUrl : 1})
      { "imageUrl" : "http://example.com/bar.jpg" }
      { "imageUrl" : "http://example.com/bar.png" }
      { "imageUrl" : "http://example.com/foo.jpg" }
      { "imageUrl" : "http://example.com/foo.png" }


      And there is the result we were looking for - only documents with the field populated are returned. This also only uses the index (i.e. it is a covered index query), so only the index needs to be in memory to return the results.



      This is a specialized use case and can't be used generally (see other answers for those options). In particular it should be noted that as things stand you cannot use count() in this way (for my example it will return 6 not 4), so please only use when appropriate.






      share|improve this answer
























      • text fields are always sparse indexes, you don't have to specify that explicitly. just my 2 cents.

        – alianos-
        Sep 22 '17 at 9:30



















      2














      db.<collectionName>.find({"IMAGE URL":{"$exists":"true"}, "IMAGE URL": {$ne: null}})





      share|improve this answer
























      • Is this a valid Json document? Two properties with the same name in the query document. Not sure how you would build that in memory if you had to.

        – BrentR
        Oct 2 '18 at 15:21



















      -4














      the Query Will be



      db.mycollection.find({"IMAGE URL":{"$exists":"true"}})


      it will return all documents having "IMAGE URL" as a key ...........






      share|improve this answer



















      • 8





        this does not work if doc['IMAGE URL'] = null

        – kilianc
        Jul 8 '14 at 13:05











      • @kilianc That's not true. $exists will capture null-values as well. See docs.mongodb.org/manual/reference/operator/query/exists

        – dorvak
        Feb 15 '16 at 10:00











      • @dorvak exactly, that's why that won't satisfy the use case in the question.

        – kilianc
        Feb 15 '16 at 17:34











      • This answer is wrong because $exists checks for the key "IMAGE URL" and does not consider it's value (null) and will return a document with "IMAGE URL" : null.

        – David Hariri
        Jun 16 '16 at 1:41











      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%2f4057196%2fhow-do-you-query-for-is-not-null-in-mongo%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      8 Answers
      8






      active

      oldest

      votes








      8 Answers
      8






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      651














      This will return all documents with a key called "IMAGE URL", but they may still have a null value.



      db.mycollection.find({"IMAGE URL":{$exists:true}});


      This will return all documents with both a key called "IMAGE URL" and a non-null value.



      db.mycollection.find({"IMAGE URL":{$ne:null}});


      Also, according to the docs, $exists currently can't use an index, but $ne can.



      Edit: Adding some examples due to interest in this answer



      Given these inserts:



      db.test.insert({"num":1, "check":"check value"});
      db.test.insert({"num":2, "check":null});
      db.test.insert({"num":3});


      This will return all three documents:



      db.test.find();


      This will return the first and second documents only:



      db.test.find({"check":{$exists:true}});


      This will return the first document only:



      db.test.find({"check":{$ne:null}});


      This will return the second and third documents only:



      db.test.find({"check":null})





      share|improve this answer





















      • 6





        It's the correct answer!

        – redice
        Dec 6 '13 at 6:54






      • 11





        According to docs, $ne includes documents that do not contain the field. Has this changed since you posted the answer? docs.mongodb.org/manual/reference/operator/query/ne

        – Andrew Mao
        Sep 24 '14 at 4:40






      • 2





        I don't believe that has changed. When checking $ne, the value is checked in all documents, including those that don't contain the field, but $ne:null still will not match a document that does not contain the field since the value of the field is still null, even though the field doesn't exist in that document.

        – Tim Gautier
        Sep 24 '14 at 17:39






      • 2





        How do you just match the second document?

        – B T
        Apr 21 '16 at 7:34






      • 1





        @River I checked when I wrote this 3 years ago and, just to be sure, I just installed Mongo and tried it out again. It still works the same way, the answer is correct. The 2nd to last query returns only the 1st document.

        – Tim Gautier
        Nov 8 '17 at 16:54
















      651














      This will return all documents with a key called "IMAGE URL", but they may still have a null value.



      db.mycollection.find({"IMAGE URL":{$exists:true}});


      This will return all documents with both a key called "IMAGE URL" and a non-null value.



      db.mycollection.find({"IMAGE URL":{$ne:null}});


      Also, according to the docs, $exists currently can't use an index, but $ne can.



      Edit: Adding some examples due to interest in this answer



      Given these inserts:



      db.test.insert({"num":1, "check":"check value"});
      db.test.insert({"num":2, "check":null});
      db.test.insert({"num":3});


      This will return all three documents:



      db.test.find();


      This will return the first and second documents only:



      db.test.find({"check":{$exists:true}});


      This will return the first document only:



      db.test.find({"check":{$ne:null}});


      This will return the second and third documents only:



      db.test.find({"check":null})





      share|improve this answer





















      • 6





        It's the correct answer!

        – redice
        Dec 6 '13 at 6:54






      • 11





        According to docs, $ne includes documents that do not contain the field. Has this changed since you posted the answer? docs.mongodb.org/manual/reference/operator/query/ne

        – Andrew Mao
        Sep 24 '14 at 4:40






      • 2





        I don't believe that has changed. When checking $ne, the value is checked in all documents, including those that don't contain the field, but $ne:null still will not match a document that does not contain the field since the value of the field is still null, even though the field doesn't exist in that document.

        – Tim Gautier
        Sep 24 '14 at 17:39






      • 2





        How do you just match the second document?

        – B T
        Apr 21 '16 at 7:34






      • 1





        @River I checked when I wrote this 3 years ago and, just to be sure, I just installed Mongo and tried it out again. It still works the same way, the answer is correct. The 2nd to last query returns only the 1st document.

        – Tim Gautier
        Nov 8 '17 at 16:54














      651












      651








      651







      This will return all documents with a key called "IMAGE URL", but they may still have a null value.



      db.mycollection.find({"IMAGE URL":{$exists:true}});


      This will return all documents with both a key called "IMAGE URL" and a non-null value.



      db.mycollection.find({"IMAGE URL":{$ne:null}});


      Also, according to the docs, $exists currently can't use an index, but $ne can.



      Edit: Adding some examples due to interest in this answer



      Given these inserts:



      db.test.insert({"num":1, "check":"check value"});
      db.test.insert({"num":2, "check":null});
      db.test.insert({"num":3});


      This will return all three documents:



      db.test.find();


      This will return the first and second documents only:



      db.test.find({"check":{$exists:true}});


      This will return the first document only:



      db.test.find({"check":{$ne:null}});


      This will return the second and third documents only:



      db.test.find({"check":null})





      share|improve this answer















      This will return all documents with a key called "IMAGE URL", but they may still have a null value.



      db.mycollection.find({"IMAGE URL":{$exists:true}});


      This will return all documents with both a key called "IMAGE URL" and a non-null value.



      db.mycollection.find({"IMAGE URL":{$ne:null}});


      Also, according to the docs, $exists currently can't use an index, but $ne can.



      Edit: Adding some examples due to interest in this answer



      Given these inserts:



      db.test.insert({"num":1, "check":"check value"});
      db.test.insert({"num":2, "check":null});
      db.test.insert({"num":3});


      This will return all three documents:



      db.test.find();


      This will return the first and second documents only:



      db.test.find({"check":{$exists:true}});


      This will return the first document only:



      db.test.find({"check":{$ne:null}});


      This will return the second and third documents only:



      db.test.find({"check":null})






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Sep 24 '14 at 17:53

























      answered Mar 15 '11 at 17:32









      Tim GautierTim Gautier

      20.9k33847




      20.9k33847








      • 6





        It's the correct answer!

        – redice
        Dec 6 '13 at 6:54






      • 11





        According to docs, $ne includes documents that do not contain the field. Has this changed since you posted the answer? docs.mongodb.org/manual/reference/operator/query/ne

        – Andrew Mao
        Sep 24 '14 at 4:40






      • 2





        I don't believe that has changed. When checking $ne, the value is checked in all documents, including those that don't contain the field, but $ne:null still will not match a document that does not contain the field since the value of the field is still null, even though the field doesn't exist in that document.

        – Tim Gautier
        Sep 24 '14 at 17:39






      • 2





        How do you just match the second document?

        – B T
        Apr 21 '16 at 7:34






      • 1





        @River I checked when I wrote this 3 years ago and, just to be sure, I just installed Mongo and tried it out again. It still works the same way, the answer is correct. The 2nd to last query returns only the 1st document.

        – Tim Gautier
        Nov 8 '17 at 16:54














      • 6





        It's the correct answer!

        – redice
        Dec 6 '13 at 6:54






      • 11





        According to docs, $ne includes documents that do not contain the field. Has this changed since you posted the answer? docs.mongodb.org/manual/reference/operator/query/ne

        – Andrew Mao
        Sep 24 '14 at 4:40






      • 2





        I don't believe that has changed. When checking $ne, the value is checked in all documents, including those that don't contain the field, but $ne:null still will not match a document that does not contain the field since the value of the field is still null, even though the field doesn't exist in that document.

        – Tim Gautier
        Sep 24 '14 at 17:39






      • 2





        How do you just match the second document?

        – B T
        Apr 21 '16 at 7:34






      • 1





        @River I checked when I wrote this 3 years ago and, just to be sure, I just installed Mongo and tried it out again. It still works the same way, the answer is correct. The 2nd to last query returns only the 1st document.

        – Tim Gautier
        Nov 8 '17 at 16:54








      6




      6





      It's the correct answer!

      – redice
      Dec 6 '13 at 6:54





      It's the correct answer!

      – redice
      Dec 6 '13 at 6:54




      11




      11





      According to docs, $ne includes documents that do not contain the field. Has this changed since you posted the answer? docs.mongodb.org/manual/reference/operator/query/ne

      – Andrew Mao
      Sep 24 '14 at 4:40





      According to docs, $ne includes documents that do not contain the field. Has this changed since you posted the answer? docs.mongodb.org/manual/reference/operator/query/ne

      – Andrew Mao
      Sep 24 '14 at 4:40




      2




      2





      I don't believe that has changed. When checking $ne, the value is checked in all documents, including those that don't contain the field, but $ne:null still will not match a document that does not contain the field since the value of the field is still null, even though the field doesn't exist in that document.

      – Tim Gautier
      Sep 24 '14 at 17:39





      I don't believe that has changed. When checking $ne, the value is checked in all documents, including those that don't contain the field, but $ne:null still will not match a document that does not contain the field since the value of the field is still null, even though the field doesn't exist in that document.

      – Tim Gautier
      Sep 24 '14 at 17:39




      2




      2





      How do you just match the second document?

      – B T
      Apr 21 '16 at 7:34





      How do you just match the second document?

      – B T
      Apr 21 '16 at 7:34




      1




      1





      @River I checked when I wrote this 3 years ago and, just to be sure, I just installed Mongo and tried it out again. It still works the same way, the answer is correct. The 2nd to last query returns only the 1st document.

      – Tim Gautier
      Nov 8 '17 at 16:54





      @River I checked when I wrote this 3 years ago and, just to be sure, I just installed Mongo and tried it out again. It still works the same way, the answer is correct. The 2nd to last query returns only the 1st document.

      – Tim Gautier
      Nov 8 '17 at 16:54













      37














      In pymongo you can use:



      db.mycollection.find({"IMAGE URL":{"$ne":None}});


      Because pymongo represents mongo "null" as python "None".






      share|improve this answer




























        37














        In pymongo you can use:



        db.mycollection.find({"IMAGE URL":{"$ne":None}});


        Because pymongo represents mongo "null" as python "None".






        share|improve this answer


























          37












          37








          37







          In pymongo you can use:



          db.mycollection.find({"IMAGE URL":{"$ne":None}});


          Because pymongo represents mongo "null" as python "None".






          share|improve this answer













          In pymongo you can use:



          db.mycollection.find({"IMAGE URL":{"$ne":None}});


          Because pymongo represents mongo "null" as python "None".







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jun 13 '13 at 18:14









          user2293072user2293072

          50549




          50549























              23














              One liner is the best :



              db.mycollection.find({ 'fieldname' : { $exists: true, $ne: null } });


              Here,



              mycollection : place your desired collection name



              fieldname : place your desired field name



              Explaination :



              $exists : When is true, $exists matches the documents that contain the field, including documents where the field value is null. If is false, the query returns only the documents that do not contain the field.



              $ne selects the documents where the value of the field is not equal to the specified value. This includes documents that do not contain the field.



              So in your provided case following query going to return all the documents with imageurl field exists and having not null value:



              db.mycollection.find({ 'imageurl' : { $exists: true, $ne: null } });





              share|improve this answer



















              • 2





                $exists: true is redundant, $ne: null is enough.

                – Stanislav Karakhanov
                Nov 21 '18 at 20:13
















              23














              One liner is the best :



              db.mycollection.find({ 'fieldname' : { $exists: true, $ne: null } });


              Here,



              mycollection : place your desired collection name



              fieldname : place your desired field name



              Explaination :



              $exists : When is true, $exists matches the documents that contain the field, including documents where the field value is null. If is false, the query returns only the documents that do not contain the field.



              $ne selects the documents where the value of the field is not equal to the specified value. This includes documents that do not contain the field.



              So in your provided case following query going to return all the documents with imageurl field exists and having not null value:



              db.mycollection.find({ 'imageurl' : { $exists: true, $ne: null } });





              share|improve this answer



















              • 2





                $exists: true is redundant, $ne: null is enough.

                – Stanislav Karakhanov
                Nov 21 '18 at 20:13














              23












              23








              23







              One liner is the best :



              db.mycollection.find({ 'fieldname' : { $exists: true, $ne: null } });


              Here,



              mycollection : place your desired collection name



              fieldname : place your desired field name



              Explaination :



              $exists : When is true, $exists matches the documents that contain the field, including documents where the field value is null. If is false, the query returns only the documents that do not contain the field.



              $ne selects the documents where the value of the field is not equal to the specified value. This includes documents that do not contain the field.



              So in your provided case following query going to return all the documents with imageurl field exists and having not null value:



              db.mycollection.find({ 'imageurl' : { $exists: true, $ne: null } });





              share|improve this answer













              One liner is the best :



              db.mycollection.find({ 'fieldname' : { $exists: true, $ne: null } });


              Here,



              mycollection : place your desired collection name



              fieldname : place your desired field name



              Explaination :



              $exists : When is true, $exists matches the documents that contain the field, including documents where the field value is null. If is false, the query returns only the documents that do not contain the field.



              $ne selects the documents where the value of the field is not equal to the specified value. This includes documents that do not contain the field.



              So in your provided case following query going to return all the documents with imageurl field exists and having not null value:



              db.mycollection.find({ 'imageurl' : { $exists: true, $ne: null } });






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Jul 12 '18 at 7:49









              AmiteshAmitesh

              1,5061120




              1,5061120








              • 2





                $exists: true is redundant, $ne: null is enough.

                – Stanislav Karakhanov
                Nov 21 '18 at 20:13














              • 2





                $exists: true is redundant, $ne: null is enough.

                – Stanislav Karakhanov
                Nov 21 '18 at 20:13








              2




              2





              $exists: true is redundant, $ne: null is enough.

              – Stanislav Karakhanov
              Nov 21 '18 at 20:13





              $exists: true is redundant, $ne: null is enough.

              – Stanislav Karakhanov
              Nov 21 '18 at 20:13











              13














              db.collection_name.find({"filed_name":{$exists:true}});



              fetch documents that contain this filed_name even it is null.



              My proposition:



              db.collection_name.find({"field_name":{$type:2}}) //type:2 == String


              You can check on the required attribute's type, it will return all the documents that its field_name queried contains a value because you are checking on the filed's type else if it is null the type condition doesn't match so nothing will be returned.



              N.b:if the field_name has an empty string which means "", it will be returned.It is the same behavior for
              db.collection_name.find({"filed_name":{$ne:null}});



              Extra validation:



              Okay, so we are not finished yet we need an extra condition.



              db.collection_name.
              find({ "field_name":{$type:2},$where:"this.field_name.length >0"})



              OR



              db.collection_name.
              find({ "field_name":{$ne:null},$where:"this.field_name.length >0"})



              Reference for all the types:
              https://docs.mongodb.com/manual/reference/operator/query/type/#op._S_type






              share|improve this answer






























                13














                db.collection_name.find({"filed_name":{$exists:true}});



                fetch documents that contain this filed_name even it is null.



                My proposition:



                db.collection_name.find({"field_name":{$type:2}}) //type:2 == String


                You can check on the required attribute's type, it will return all the documents that its field_name queried contains a value because you are checking on the filed's type else if it is null the type condition doesn't match so nothing will be returned.



                N.b:if the field_name has an empty string which means "", it will be returned.It is the same behavior for
                db.collection_name.find({"filed_name":{$ne:null}});



                Extra validation:



                Okay, so we are not finished yet we need an extra condition.



                db.collection_name.
                find({ "field_name":{$type:2},$where:"this.field_name.length >0"})



                OR



                db.collection_name.
                find({ "field_name":{$ne:null},$where:"this.field_name.length >0"})



                Reference for all the types:
                https://docs.mongodb.com/manual/reference/operator/query/type/#op._S_type






                share|improve this answer




























                  13












                  13








                  13







                  db.collection_name.find({"filed_name":{$exists:true}});



                  fetch documents that contain this filed_name even it is null.



                  My proposition:



                  db.collection_name.find({"field_name":{$type:2}}) //type:2 == String


                  You can check on the required attribute's type, it will return all the documents that its field_name queried contains a value because you are checking on the filed's type else if it is null the type condition doesn't match so nothing will be returned.



                  N.b:if the field_name has an empty string which means "", it will be returned.It is the same behavior for
                  db.collection_name.find({"filed_name":{$ne:null}});



                  Extra validation:



                  Okay, so we are not finished yet we need an extra condition.



                  db.collection_name.
                  find({ "field_name":{$type:2},$where:"this.field_name.length >0"})



                  OR



                  db.collection_name.
                  find({ "field_name":{$ne:null},$where:"this.field_name.length >0"})



                  Reference for all the types:
                  https://docs.mongodb.com/manual/reference/operator/query/type/#op._S_type






                  share|improve this answer















                  db.collection_name.find({"filed_name":{$exists:true}});



                  fetch documents that contain this filed_name even it is null.



                  My proposition:



                  db.collection_name.find({"field_name":{$type:2}}) //type:2 == String


                  You can check on the required attribute's type, it will return all the documents that its field_name queried contains a value because you are checking on the filed's type else if it is null the type condition doesn't match so nothing will be returned.



                  N.b:if the field_name has an empty string which means "", it will be returned.It is the same behavior for
                  db.collection_name.find({"filed_name":{$ne:null}});



                  Extra validation:



                  Okay, so we are not finished yet we need an extra condition.



                  db.collection_name.
                  find({ "field_name":{$type:2},$where:"this.field_name.length >0"})



                  OR



                  db.collection_name.
                  find({ "field_name":{$ne:null},$where:"this.field_name.length >0"})



                  Reference for all the types:
                  https://docs.mongodb.com/manual/reference/operator/query/type/#op._S_type







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Jul 18 '17 at 9:06

























                  answered Jul 16 '17 at 14:51









                  Farouk El kholyFarouk El kholy

                  24338




                  24338























                      11















                      Sharing for future readers.




                      This query worked for us (query executed from MongoDB compass):



                      {
                      "fieldName": {
                      "$nin": [
                      "",
                      null
                      ]
                      }
                      }





                      share|improve this answer





















                      • 1





                        { $exists: true, $ne: null } didn't show correct result. Your query works good

                        – Oleksandr Buchek
                        Aug 8 '18 at 15:59











                      • @OleksandrBuchek Glad to know this man!

                        – student
                        Aug 9 '18 at 12:01
















                      11















                      Sharing for future readers.




                      This query worked for us (query executed from MongoDB compass):



                      {
                      "fieldName": {
                      "$nin": [
                      "",
                      null
                      ]
                      }
                      }





                      share|improve this answer





















                      • 1





                        { $exists: true, $ne: null } didn't show correct result. Your query works good

                        – Oleksandr Buchek
                        Aug 8 '18 at 15:59











                      • @OleksandrBuchek Glad to know this man!

                        – student
                        Aug 9 '18 at 12:01














                      11












                      11








                      11








                      Sharing for future readers.




                      This query worked for us (query executed from MongoDB compass):



                      {
                      "fieldName": {
                      "$nin": [
                      "",
                      null
                      ]
                      }
                      }





                      share|improve this answer
















                      Sharing for future readers.




                      This query worked for us (query executed from MongoDB compass):



                      {
                      "fieldName": {
                      "$nin": [
                      "",
                      null
                      ]
                      }
                      }






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Jan 10 '18 at 12:02

























                      answered Jan 10 '18 at 8:58









                      studentstudent

                      11.4k973133




                      11.4k973133








                      • 1





                        { $exists: true, $ne: null } didn't show correct result. Your query works good

                        – Oleksandr Buchek
                        Aug 8 '18 at 15:59











                      • @OleksandrBuchek Glad to know this man!

                        – student
                        Aug 9 '18 at 12:01














                      • 1





                        { $exists: true, $ne: null } didn't show correct result. Your query works good

                        – Oleksandr Buchek
                        Aug 8 '18 at 15:59











                      • @OleksandrBuchek Glad to know this man!

                        – student
                        Aug 9 '18 at 12:01








                      1




                      1





                      { $exists: true, $ne: null } didn't show correct result. Your query works good

                      – Oleksandr Buchek
                      Aug 8 '18 at 15:59





                      { $exists: true, $ne: null } didn't show correct result. Your query works good

                      – Oleksandr Buchek
                      Aug 8 '18 at 15:59













                      @OleksandrBuchek Glad to know this man!

                      – student
                      Aug 9 '18 at 12:01





                      @OleksandrBuchek Glad to know this man!

                      – student
                      Aug 9 '18 at 12:01











                      2














                      An alternative that has not been mentioned, but that may be a more efficient option for some (won't work with NULL entries) is to use a sparse index (entries in the index only exist when there is something in the field). Here is a sample data set:



                      db.foo.find()
                      { "_id" : ObjectId("544540b31b5cf91c4893eb94"), "imageUrl" : "http://example.com/foo.jpg" }
                      { "_id" : ObjectId("544540ba1b5cf91c4893eb95"), "imageUrl" : "http://example.com/bar.jpg" }
                      { "_id" : ObjectId("544540c51b5cf91c4893eb96"), "imageUrl" : "http://example.com/foo.png" }
                      { "_id" : ObjectId("544540c91b5cf91c4893eb97"), "imageUrl" : "http://example.com/bar.png" }
                      { "_id" : ObjectId("544540ed1b5cf91c4893eb98"), "otherField" : 1 }
                      { "_id" : ObjectId("544540f11b5cf91c4893eb99"), "otherField" : 2 }


                      Now, create the sparse index on imageUrl field:



                      db.foo.ensureIndex( { "imageUrl": 1 }, { sparse: true } )
                      {
                      "createdCollectionAutomatically" : false,
                      "numIndexesBefore" : 1,
                      "numIndexesAfter" : 2,
                      "ok" : 1
                      }


                      Now, there is always a chance (and in particular with a small data set like my sample) that rather than using an index, MongoDB will use a table scan, even for a potential covered index query. As it turns out that gives me an easy way to illustrate the difference here:



                      db.foo.find({}, {_id : 0, imageUrl : 1})
                      { "imageUrl" : "http://example.com/foo.jpg" }
                      { "imageUrl" : "http://example.com/bar.jpg" }
                      { "imageUrl" : "http://example.com/foo.png" }
                      { "imageUrl" : "http://example.com/bar.png" }
                      { }
                      { }


                      OK, so the extra documents with no imageUrl are being returned, just empty, not what we wanted. Just to confirm why, do an explain:



                      db.foo.find({}, {_id : 0, imageUrl : 1}).explain()
                      {
                      "cursor" : "BasicCursor",
                      "isMultiKey" : false,
                      "n" : 6,
                      "nscannedObjects" : 6,
                      "nscanned" : 6,
                      "nscannedObjectsAllPlans" : 6,
                      "nscannedAllPlans" : 6,
                      "scanAndOrder" : false,
                      "indexOnly" : false,
                      "nYields" : 0,
                      "nChunkSkips" : 0,
                      "millis" : 0,
                      "server" : "localhost:31100",
                      "filterSet" : false
                      }


                      So, yes, a BasicCursor equals a table scan, it did not use the index. Let's force the query to use our sparse index with a hint():



                      db.foo.find({}, {_id : 0, imageUrl : 1}).hint({imageUrl : 1})
                      { "imageUrl" : "http://example.com/bar.jpg" }
                      { "imageUrl" : "http://example.com/bar.png" }
                      { "imageUrl" : "http://example.com/foo.jpg" }
                      { "imageUrl" : "http://example.com/foo.png" }


                      And there is the result we were looking for - only documents with the field populated are returned. This also only uses the index (i.e. it is a covered index query), so only the index needs to be in memory to return the results.



                      This is a specialized use case and can't be used generally (see other answers for those options). In particular it should be noted that as things stand you cannot use count() in this way (for my example it will return 6 not 4), so please only use when appropriate.






                      share|improve this answer
























                      • text fields are always sparse indexes, you don't have to specify that explicitly. just my 2 cents.

                        – alianos-
                        Sep 22 '17 at 9:30
















                      2














                      An alternative that has not been mentioned, but that may be a more efficient option for some (won't work with NULL entries) is to use a sparse index (entries in the index only exist when there is something in the field). Here is a sample data set:



                      db.foo.find()
                      { "_id" : ObjectId("544540b31b5cf91c4893eb94"), "imageUrl" : "http://example.com/foo.jpg" }
                      { "_id" : ObjectId("544540ba1b5cf91c4893eb95"), "imageUrl" : "http://example.com/bar.jpg" }
                      { "_id" : ObjectId("544540c51b5cf91c4893eb96"), "imageUrl" : "http://example.com/foo.png" }
                      { "_id" : ObjectId("544540c91b5cf91c4893eb97"), "imageUrl" : "http://example.com/bar.png" }
                      { "_id" : ObjectId("544540ed1b5cf91c4893eb98"), "otherField" : 1 }
                      { "_id" : ObjectId("544540f11b5cf91c4893eb99"), "otherField" : 2 }


                      Now, create the sparse index on imageUrl field:



                      db.foo.ensureIndex( { "imageUrl": 1 }, { sparse: true } )
                      {
                      "createdCollectionAutomatically" : false,
                      "numIndexesBefore" : 1,
                      "numIndexesAfter" : 2,
                      "ok" : 1
                      }


                      Now, there is always a chance (and in particular with a small data set like my sample) that rather than using an index, MongoDB will use a table scan, even for a potential covered index query. As it turns out that gives me an easy way to illustrate the difference here:



                      db.foo.find({}, {_id : 0, imageUrl : 1})
                      { "imageUrl" : "http://example.com/foo.jpg" }
                      { "imageUrl" : "http://example.com/bar.jpg" }
                      { "imageUrl" : "http://example.com/foo.png" }
                      { "imageUrl" : "http://example.com/bar.png" }
                      { }
                      { }


                      OK, so the extra documents with no imageUrl are being returned, just empty, not what we wanted. Just to confirm why, do an explain:



                      db.foo.find({}, {_id : 0, imageUrl : 1}).explain()
                      {
                      "cursor" : "BasicCursor",
                      "isMultiKey" : false,
                      "n" : 6,
                      "nscannedObjects" : 6,
                      "nscanned" : 6,
                      "nscannedObjectsAllPlans" : 6,
                      "nscannedAllPlans" : 6,
                      "scanAndOrder" : false,
                      "indexOnly" : false,
                      "nYields" : 0,
                      "nChunkSkips" : 0,
                      "millis" : 0,
                      "server" : "localhost:31100",
                      "filterSet" : false
                      }


                      So, yes, a BasicCursor equals a table scan, it did not use the index. Let's force the query to use our sparse index with a hint():



                      db.foo.find({}, {_id : 0, imageUrl : 1}).hint({imageUrl : 1})
                      { "imageUrl" : "http://example.com/bar.jpg" }
                      { "imageUrl" : "http://example.com/bar.png" }
                      { "imageUrl" : "http://example.com/foo.jpg" }
                      { "imageUrl" : "http://example.com/foo.png" }


                      And there is the result we were looking for - only documents with the field populated are returned. This also only uses the index (i.e. it is a covered index query), so only the index needs to be in memory to return the results.



                      This is a specialized use case and can't be used generally (see other answers for those options). In particular it should be noted that as things stand you cannot use count() in this way (for my example it will return 6 not 4), so please only use when appropriate.






                      share|improve this answer
























                      • text fields are always sparse indexes, you don't have to specify that explicitly. just my 2 cents.

                        – alianos-
                        Sep 22 '17 at 9:30














                      2












                      2








                      2







                      An alternative that has not been mentioned, but that may be a more efficient option for some (won't work with NULL entries) is to use a sparse index (entries in the index only exist when there is something in the field). Here is a sample data set:



                      db.foo.find()
                      { "_id" : ObjectId("544540b31b5cf91c4893eb94"), "imageUrl" : "http://example.com/foo.jpg" }
                      { "_id" : ObjectId("544540ba1b5cf91c4893eb95"), "imageUrl" : "http://example.com/bar.jpg" }
                      { "_id" : ObjectId("544540c51b5cf91c4893eb96"), "imageUrl" : "http://example.com/foo.png" }
                      { "_id" : ObjectId("544540c91b5cf91c4893eb97"), "imageUrl" : "http://example.com/bar.png" }
                      { "_id" : ObjectId("544540ed1b5cf91c4893eb98"), "otherField" : 1 }
                      { "_id" : ObjectId("544540f11b5cf91c4893eb99"), "otherField" : 2 }


                      Now, create the sparse index on imageUrl field:



                      db.foo.ensureIndex( { "imageUrl": 1 }, { sparse: true } )
                      {
                      "createdCollectionAutomatically" : false,
                      "numIndexesBefore" : 1,
                      "numIndexesAfter" : 2,
                      "ok" : 1
                      }


                      Now, there is always a chance (and in particular with a small data set like my sample) that rather than using an index, MongoDB will use a table scan, even for a potential covered index query. As it turns out that gives me an easy way to illustrate the difference here:



                      db.foo.find({}, {_id : 0, imageUrl : 1})
                      { "imageUrl" : "http://example.com/foo.jpg" }
                      { "imageUrl" : "http://example.com/bar.jpg" }
                      { "imageUrl" : "http://example.com/foo.png" }
                      { "imageUrl" : "http://example.com/bar.png" }
                      { }
                      { }


                      OK, so the extra documents with no imageUrl are being returned, just empty, not what we wanted. Just to confirm why, do an explain:



                      db.foo.find({}, {_id : 0, imageUrl : 1}).explain()
                      {
                      "cursor" : "BasicCursor",
                      "isMultiKey" : false,
                      "n" : 6,
                      "nscannedObjects" : 6,
                      "nscanned" : 6,
                      "nscannedObjectsAllPlans" : 6,
                      "nscannedAllPlans" : 6,
                      "scanAndOrder" : false,
                      "indexOnly" : false,
                      "nYields" : 0,
                      "nChunkSkips" : 0,
                      "millis" : 0,
                      "server" : "localhost:31100",
                      "filterSet" : false
                      }


                      So, yes, a BasicCursor equals a table scan, it did not use the index. Let's force the query to use our sparse index with a hint():



                      db.foo.find({}, {_id : 0, imageUrl : 1}).hint({imageUrl : 1})
                      { "imageUrl" : "http://example.com/bar.jpg" }
                      { "imageUrl" : "http://example.com/bar.png" }
                      { "imageUrl" : "http://example.com/foo.jpg" }
                      { "imageUrl" : "http://example.com/foo.png" }


                      And there is the result we were looking for - only documents with the field populated are returned. This also only uses the index (i.e. it is a covered index query), so only the index needs to be in memory to return the results.



                      This is a specialized use case and can't be used generally (see other answers for those options). In particular it should be noted that as things stand you cannot use count() in this way (for my example it will return 6 not 4), so please only use when appropriate.






                      share|improve this answer













                      An alternative that has not been mentioned, but that may be a more efficient option for some (won't work with NULL entries) is to use a sparse index (entries in the index only exist when there is something in the field). Here is a sample data set:



                      db.foo.find()
                      { "_id" : ObjectId("544540b31b5cf91c4893eb94"), "imageUrl" : "http://example.com/foo.jpg" }
                      { "_id" : ObjectId("544540ba1b5cf91c4893eb95"), "imageUrl" : "http://example.com/bar.jpg" }
                      { "_id" : ObjectId("544540c51b5cf91c4893eb96"), "imageUrl" : "http://example.com/foo.png" }
                      { "_id" : ObjectId("544540c91b5cf91c4893eb97"), "imageUrl" : "http://example.com/bar.png" }
                      { "_id" : ObjectId("544540ed1b5cf91c4893eb98"), "otherField" : 1 }
                      { "_id" : ObjectId("544540f11b5cf91c4893eb99"), "otherField" : 2 }


                      Now, create the sparse index on imageUrl field:



                      db.foo.ensureIndex( { "imageUrl": 1 }, { sparse: true } )
                      {
                      "createdCollectionAutomatically" : false,
                      "numIndexesBefore" : 1,
                      "numIndexesAfter" : 2,
                      "ok" : 1
                      }


                      Now, there is always a chance (and in particular with a small data set like my sample) that rather than using an index, MongoDB will use a table scan, even for a potential covered index query. As it turns out that gives me an easy way to illustrate the difference here:



                      db.foo.find({}, {_id : 0, imageUrl : 1})
                      { "imageUrl" : "http://example.com/foo.jpg" }
                      { "imageUrl" : "http://example.com/bar.jpg" }
                      { "imageUrl" : "http://example.com/foo.png" }
                      { "imageUrl" : "http://example.com/bar.png" }
                      { }
                      { }


                      OK, so the extra documents with no imageUrl are being returned, just empty, not what we wanted. Just to confirm why, do an explain:



                      db.foo.find({}, {_id : 0, imageUrl : 1}).explain()
                      {
                      "cursor" : "BasicCursor",
                      "isMultiKey" : false,
                      "n" : 6,
                      "nscannedObjects" : 6,
                      "nscanned" : 6,
                      "nscannedObjectsAllPlans" : 6,
                      "nscannedAllPlans" : 6,
                      "scanAndOrder" : false,
                      "indexOnly" : false,
                      "nYields" : 0,
                      "nChunkSkips" : 0,
                      "millis" : 0,
                      "server" : "localhost:31100",
                      "filterSet" : false
                      }


                      So, yes, a BasicCursor equals a table scan, it did not use the index. Let's force the query to use our sparse index with a hint():



                      db.foo.find({}, {_id : 0, imageUrl : 1}).hint({imageUrl : 1})
                      { "imageUrl" : "http://example.com/bar.jpg" }
                      { "imageUrl" : "http://example.com/bar.png" }
                      { "imageUrl" : "http://example.com/foo.jpg" }
                      { "imageUrl" : "http://example.com/foo.png" }


                      And there is the result we were looking for - only documents with the field populated are returned. This also only uses the index (i.e. it is a covered index query), so only the index needs to be in memory to return the results.



                      This is a specialized use case and can't be used generally (see other answers for those options). In particular it should be noted that as things stand you cannot use count() in this way (for my example it will return 6 not 4), so please only use when appropriate.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Oct 20 '14 at 17:31









                      Adam ComerfordAdam Comerford

                      17.3k34574




                      17.3k34574













                      • text fields are always sparse indexes, you don't have to specify that explicitly. just my 2 cents.

                        – alianos-
                        Sep 22 '17 at 9:30



















                      • text fields are always sparse indexes, you don't have to specify that explicitly. just my 2 cents.

                        – alianos-
                        Sep 22 '17 at 9:30

















                      text fields are always sparse indexes, you don't have to specify that explicitly. just my 2 cents.

                      – alianos-
                      Sep 22 '17 at 9:30





                      text fields are always sparse indexes, you don't have to specify that explicitly. just my 2 cents.

                      – alianos-
                      Sep 22 '17 at 9:30











                      2














                      db.<collectionName>.find({"IMAGE URL":{"$exists":"true"}, "IMAGE URL": {$ne: null}})





                      share|improve this answer
























                      • Is this a valid Json document? Two properties with the same name in the query document. Not sure how you would build that in memory if you had to.

                        – BrentR
                        Oct 2 '18 at 15:21
















                      2














                      db.<collectionName>.find({"IMAGE URL":{"$exists":"true"}, "IMAGE URL": {$ne: null}})





                      share|improve this answer
























                      • Is this a valid Json document? Two properties with the same name in the query document. Not sure how you would build that in memory if you had to.

                        – BrentR
                        Oct 2 '18 at 15:21














                      2












                      2








                      2







                      db.<collectionName>.find({"IMAGE URL":{"$exists":"true"}, "IMAGE URL": {$ne: null}})





                      share|improve this answer













                      db.<collectionName>.find({"IMAGE URL":{"$exists":"true"}, "IMAGE URL": {$ne: null}})






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Mar 6 '18 at 14:52









                      Berhanu TarekegnBerhanu Tarekegn

                      1725




                      1725













                      • Is this a valid Json document? Two properties with the same name in the query document. Not sure how you would build that in memory if you had to.

                        – BrentR
                        Oct 2 '18 at 15:21



















                      • Is this a valid Json document? Two properties with the same name in the query document. Not sure how you would build that in memory if you had to.

                        – BrentR
                        Oct 2 '18 at 15:21

















                      Is this a valid Json document? Two properties with the same name in the query document. Not sure how you would build that in memory if you had to.

                      – BrentR
                      Oct 2 '18 at 15:21





                      Is this a valid Json document? Two properties with the same name in the query document. Not sure how you would build that in memory if you had to.

                      – BrentR
                      Oct 2 '18 at 15:21











                      -4














                      the Query Will be



                      db.mycollection.find({"IMAGE URL":{"$exists":"true"}})


                      it will return all documents having "IMAGE URL" as a key ...........






                      share|improve this answer



















                      • 8





                        this does not work if doc['IMAGE URL'] = null

                        – kilianc
                        Jul 8 '14 at 13:05











                      • @kilianc That's not true. $exists will capture null-values as well. See docs.mongodb.org/manual/reference/operator/query/exists

                        – dorvak
                        Feb 15 '16 at 10:00











                      • @dorvak exactly, that's why that won't satisfy the use case in the question.

                        – kilianc
                        Feb 15 '16 at 17:34











                      • This answer is wrong because $exists checks for the key "IMAGE URL" and does not consider it's value (null) and will return a document with "IMAGE URL" : null.

                        – David Hariri
                        Jun 16 '16 at 1:41
















                      -4














                      the Query Will be



                      db.mycollection.find({"IMAGE URL":{"$exists":"true"}})


                      it will return all documents having "IMAGE URL" as a key ...........






                      share|improve this answer



















                      • 8





                        this does not work if doc['IMAGE URL'] = null

                        – kilianc
                        Jul 8 '14 at 13:05











                      • @kilianc That's not true. $exists will capture null-values as well. See docs.mongodb.org/manual/reference/operator/query/exists

                        – dorvak
                        Feb 15 '16 at 10:00











                      • @dorvak exactly, that's why that won't satisfy the use case in the question.

                        – kilianc
                        Feb 15 '16 at 17:34











                      • This answer is wrong because $exists checks for the key "IMAGE URL" and does not consider it's value (null) and will return a document with "IMAGE URL" : null.

                        – David Hariri
                        Jun 16 '16 at 1:41














                      -4












                      -4








                      -4







                      the Query Will be



                      db.mycollection.find({"IMAGE URL":{"$exists":"true"}})


                      it will return all documents having "IMAGE URL" as a key ...........






                      share|improve this answer













                      the Query Will be



                      db.mycollection.find({"IMAGE URL":{"$exists":"true"}})


                      it will return all documents having "IMAGE URL" as a key ...........







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Jan 4 '11 at 6:43









                      Sagar VarpeSagar Varpe

                      1,73441939




                      1,73441939








                      • 8





                        this does not work if doc['IMAGE URL'] = null

                        – kilianc
                        Jul 8 '14 at 13:05











                      • @kilianc That's not true. $exists will capture null-values as well. See docs.mongodb.org/manual/reference/operator/query/exists

                        – dorvak
                        Feb 15 '16 at 10:00











                      • @dorvak exactly, that's why that won't satisfy the use case in the question.

                        – kilianc
                        Feb 15 '16 at 17:34











                      • This answer is wrong because $exists checks for the key "IMAGE URL" and does not consider it's value (null) and will return a document with "IMAGE URL" : null.

                        – David Hariri
                        Jun 16 '16 at 1:41














                      • 8





                        this does not work if doc['IMAGE URL'] = null

                        – kilianc
                        Jul 8 '14 at 13:05











                      • @kilianc That's not true. $exists will capture null-values as well. See docs.mongodb.org/manual/reference/operator/query/exists

                        – dorvak
                        Feb 15 '16 at 10:00











                      • @dorvak exactly, that's why that won't satisfy the use case in the question.

                        – kilianc
                        Feb 15 '16 at 17:34











                      • This answer is wrong because $exists checks for the key "IMAGE URL" and does not consider it's value (null) and will return a document with "IMAGE URL" : null.

                        – David Hariri
                        Jun 16 '16 at 1:41








                      8




                      8





                      this does not work if doc['IMAGE URL'] = null

                      – kilianc
                      Jul 8 '14 at 13:05





                      this does not work if doc['IMAGE URL'] = null

                      – kilianc
                      Jul 8 '14 at 13:05













                      @kilianc That's not true. $exists will capture null-values as well. See docs.mongodb.org/manual/reference/operator/query/exists

                      – dorvak
                      Feb 15 '16 at 10:00





                      @kilianc That's not true. $exists will capture null-values as well. See docs.mongodb.org/manual/reference/operator/query/exists

                      – dorvak
                      Feb 15 '16 at 10:00













                      @dorvak exactly, that's why that won't satisfy the use case in the question.

                      – kilianc
                      Feb 15 '16 at 17:34





                      @dorvak exactly, that's why that won't satisfy the use case in the question.

                      – kilianc
                      Feb 15 '16 at 17:34













                      This answer is wrong because $exists checks for the key "IMAGE URL" and does not consider it's value (null) and will return a document with "IMAGE URL" : null.

                      – David Hariri
                      Jun 16 '16 at 1:41





                      This answer is wrong because $exists checks for the key "IMAGE URL" and does not consider it's value (null) and will return a document with "IMAGE URL" : null.

                      – David Hariri
                      Jun 16 '16 at 1:41


















                      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%2f4057196%2fhow-do-you-query-for-is-not-null-in-mongo%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

                      Guess what letter conforming each word

                      Port of Spain

                      Run scheduled task as local user group (not BUILTIN)