我目前正在试验mongodb,也就是mongoshell。
我希望获取tomato.consensus的值为null的文档。以下查询匹配1991年的文档。
db.movieDetails.find({"tomato.consensus": null}但一个不受欢迎的副作用是,它还将返回不带tomato.concensus字段的文档和根本不带西红柿字段的文档。
我的想法是使用$exists操作器。该查询返回362个文档。
db.movieDetails.find({"tomato.consensus": {$exists: true})我最初的想法是获取带有a字段tomato.consensus的文档,然后使用"tomato.consensus":null来提取文档。(不起作用):
$db.movieDetails.find({"tomato.consensus": {$exists: true}}).find({"tomato.consensus": null})
$uncaught exception: TypeError: db.movieDetails.find(...).find is not a function :是否有一种语法允许同时执行这两种操作,以消除mongo shell中的副作用?
发布于 2020-01-15 17:24:25
您只需要一个$and运算符并定义多个过滤条件
db.movieDetails.find( { $and: [ {"tomato.consensus": { $exists: true } }, {"tomato.consensus": null } ] } )查找有关$and运算符和示例here的更多详细信息。
发布于 2020-01-15 17:23:36
您确实可以结合使用$exist和$and来检索存在"tomato.consensus“且为null的文档:
db.movieDetails.find( { $and: [ { "tomato.consensus": null }, { "tomato.consensus": { $exists: true } } ] } )发布于 2020-01-15 17:28:34
这是null和undefined之间的主要区别。undefined始终为null,但在某些情况下,null并不是未定义的。
检查undefined可以通过以下方式完成:
1) {"tomato.consensus": { $exists: false } }
2) {"tomato.consensus": undefined }
因此,为了搜索空值而不是未定义的值,请使用query:
{'tomato.consensus': null, 'tomato.consensus': { $exists : true }}https://stackoverflow.com/questions/59748193
复制相似问题