我想执行以下查询:
db.mycollection.find(HAS IMAGE URL)
正确的语法应该是什么?
{ field : {$ne : null} }
检查非空 docs.mongodb.org/manual/reference/operator/query/ne
这将返回所有带有一个名为“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})
一个班轮是最好的:
db.mycollection.find({ 'fieldname' : { $exists: true, $ne: null } });
这里,
mycollection :放置您想要的集合名称
fieldname :放置您想要的字段名称
说明:
$exists :当为真时,$exists 匹配包含该字段的文档,包括字段值为空的文档。如果为 false,则查询仅返回不包含该字段的文档。
$ne 选择字段值不等于指定值的文档。这包括不包含该字段的文档。
因此,在您提供的情况下,以下查询将返回所有具有 imageurl 字段且不具有空值的文档:
db.mycollection.find({ 'imageurl' : { $exists: true, $ne: null } });
$exists: true
是多余的,$ne: null
就足够了。
$exists: true
也返回 null
值。必须同时存在 $exists: true
和 $ne: null
。这不是多余的。
$ne: null
使 $exists: true
— 由您自己解释!由于 $exists: true
返回 null
值,而 $ne: null
将其过滤掉,因此您只需要 $ne: null
在 pymongo 中,您可以使用:
db.mycollection.find({"IMAGE URL":{"$ne":None}});
因为pymongo将mongo“null”表示为python“None”。
db.collection_name.find({"filed_name":{$exists:true}});
获取包含此字段名称的文档,即使它为空。
警告
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
]
}
}
在理想情况下,您希望测试所有三个值,null、"" 或空(记录中不存在字段)
您可以执行以下操作。
db.users.find({$and: [{"name" : {$nin: ["", null]}}, {"name" : {$exists: true}}]})
检查 mongo compass 中是否存在列的最简单方法是:
{ 'column_name': { $exists: true } }
尚未提及的替代方法,但对于某些人(不适用于 NULL 条目)可能是更有效的选择是使用 sparse index(索引中的条目仅在字段中有内容时才存在)。这是一个示例数据集:
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 }
现在,在 imageUrl 字段上创建稀疏索引:
db.foo.ensureIndex( { "imageUrl": 1 }, { sparse: true } )
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
现在,MongoDB 总是有机会(尤其是像我的示例这样的小数据集)而不是使用索引,而是使用表扫描,即使对于潜在的覆盖索引查询也是如此。事实证明,这给了我一个简单的方法来说明这里的区别:
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" }
{ }
{ }
好的,所以没有 imageUrl
的额外文档被返回,只是空的,不是我们想要的。只是为了确认原因,请解释一下:
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
}
所以,是的,BasicCursor
等于表扫描,它没有使用索引。让我们强制查询使用带有 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" }
这就是我们正在寻找的结果——只返回填充了字段的文档。这也只使用了索引(即它是一个覆盖索引查询),所以只有索引需要在内存中才能返回结果。
这是一个专门的用例,不能普遍使用(请参阅这些选项的其他答案)。特别要注意的是,就目前情况而言,您不能以这种方式使用 count()
(对于我的示例,它将返回 6 而不是 4),因此请仅在适当的时候使用。
db.<collectionName>.find({"IMAGE URL":{"$exists":"true"}, "IMAGE URL": {$ne: null}})
感谢您提供解决方案,我在 MQL 中注意到,有时 $ne:null 不起作用,而是我们需要使用语法 $ne:"" 即在上面示例的上下文中我们需要使用 db.mycollection.find({"IMAGE URL":{"$ne":""}})
- 不知道为什么发生这种情况,我已经在 MongoDB 论坛上发布了这个问题。
以下是显示示例的快照:
https://i.stack.imgur.com/sV5sE.png
查询将是
db.mycollection.find({"IMAGE URL":{"$exists":"true"}})
它将返回所有以“IMAGE URL”为键的文档............
$exists
检查键 "IMAGE URL"
并且不考虑它的值 (null
),并且将返回带有 "IMAGE URL" : null
的文档。
不定期副业成功案例分享
$ne
包括不包含该字段的文档。自从您发布答案以来,这种情况发生了变化吗? docs.mongodb.org/manual/reference/operator/query/ne