我想执行以下查询:

db.mycollection.find(HAS IMAGE URL)

正确的语法应该是什么?


当前回答

查询将是

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

它将返回所有具有“IMAGE URL”作为密钥的文档...........

其他回答

在pymongo中,你可以使用:

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

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

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

检查mongo compass中列是否存在的最简单方法是:

{ 'column_name': { $exists: true } }

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

您可以执行以下操作。

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

这将返回所有带有“IMAGE URL”键的文档,但它们仍然可能有一个空值。

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

这将返回所有具有“IMAGE URL”键和非空值的文档。

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

此外,根据文档,$exists目前不能使用索引,但$ne可以。

编辑:由于对这个答案感兴趣,添加了一些例子

给定这些插入:

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

这将返回所有三个文档:

db.test.find();

这将只返回第一个和第二个文档:

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

这将只返回第一个文档:

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

这将只返回第二个和第三个文档:

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