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

后端 未结 10 2305
暖寄归人
暖寄归人 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 } });
    

提交回复
热议问题