MongoDB:通过不存在的字段查找文档?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8567469/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 12:23:03  来源:igfitidea点击:

MongoDB: Find a document by non-existence of a field?

mongodb

提问by k00k

Is there a way to specify a condition of "where document doesn't contain field" ?

有没有办法指定“其中文档不包含字段”的条件?

For example, I want to only find the first of these 2 because it doesn't have the "price" field.

例如,我只想找到这两个中的第一个,因为它没有“价格”字段。

{"fruit":"apple", "color":"red"}


{"fruit":"banana", "color":"yellow", "price":"2.00"}

回答by dampier

Try the $existsoperator:

试试$exists运营商:

db.mycollection.find({ "price" : { "$exists" : false } })

and see its documentation.

并查看其文档