你如何查询“不为空"?在蒙戈? [英] How do you query for "is not null" in Mongo?
本文介绍了你如何查询“不为空"?在蒙戈?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想执行以下查询:
db.mycollection.find(HAS IMAGE URL)
正确的语法应该是什么?
What should be the correct syntax?
推荐答案
这将返回具有名为IMAGE URL"的键的所有文档,但它们可能仍具有空值.
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}});
这将返回具有名为IMAGE URL"的键的所有文档;和一个非空值.
This will return all documents with both a key called "IMAGE URL" and a non-null value.
db.mycollection.find({"IMAGE URL":{$ne:null}});
此外,根据文档,$exists 目前不能使用索引,但 $ne 可以.
Also, according to the docs, $exists currently can't use an index, but $ne can.
由于对此答案感兴趣而添加了一些示例
鉴于这些插入:
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})
这篇关于你如何查询“不为空"?在蒙戈?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文