我想执行以下查询:

db.mycollection.find(HAS IMAGE URL)

正确的语法应该是什么?


当前回答

在pymongo中,你可以使用:

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

因为pymongo将mongo null表示为python None。

其他回答

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

获取包含此filed_name的文档,即使它是空的。

警告

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

获取文档,它的field_name有一个值$ne到null,但这个值也可以是空字符串。

我的建议:

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

分享给未来的读者。

这个查询为我们工作(查询从MongoDB compass执行):

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

感谢提供解决方案,我注意到在MQL中,有时$ne:null不起作用,而是我们需要使用语法$ne:"",即在上述示例的上下文中,我们需要使用db.mycollection。find({"IMAGE URL":{"$ne":""}}) -不知道为什么会发生这种情况,我已经在MongoDB论坛上发布了这个问题。

下图为示例快照:

在理想情况下,您希望测试所有三个值,null、""或空(记录中不存在字段)

您可以执行以下操作。

db.users.find({$and: [{"name" : {$nin: ["", null]}}, {"name" : {$exists: true}}]})

在pymongo中,你可以使用:

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

因为pymongo将mongo null表示为python None。