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

后端 未结 10 2172
暖寄归人
暖寄归人 2020-11-30 15:59

I would like to execute a following query:

db.mycollection.find(HAS IMAGE URL)

What should be the correct syntax?

相关标签:
10条回答
  • 2020-11-30 16:41

    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 } });
    
    0 讨论(0)
  • 2020-11-30 16:42
    db.<collectionName>.find({"IMAGE URL":{"$exists":"true"}, "IMAGE URL": {$ne: null}})
    
    0 讨论(0)
  • 2020-11-30 16:43

    In pymongo you can use:

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

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

    0 讨论(0)
  • 2020-11-30 16:46
    db.collection_name.find({"filed_name":{$exists:true}});
    

    fetch documents that contain this filed_name even it is null.

    Warning

    db.collection_name.find({"filed_name":{$ne:null}});
    

    fetch documents that its field_name has a value $ne to null but this value could be an empty string also.

    My proposition:

    db.collection_name.find({ "field_name":{$ne:null},$where:"this.field_name.length >0"})
    
    0 讨论(0)
  • 2020-11-30 16:49

    The simplest way to check the existence of the column in mongo compass is :

    { 'column_name': { $exists: true } }
    
    0 讨论(0)
  • 2020-11-30 16:52

    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})
    
    0 讨论(0)
提交回复
热议问题