如何在Mongo中查询这个? (不为null) [英] How do you query this in Mongo? (is not null)

查看:2713
本文介绍了如何在Mongo中查询这个? (不为null)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  db.mycollection.find(HAS IMAGE URL)


解决方案

这会返回带有IMAGE URL键的所有文档,但它们可能仍然为空值。

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

这将返回所有包含名为IMAGE URL >非空值。

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

此外,根据docs,$ exists目前不能使用索引, 。



编辑:由于对此答案感兴趣而添加一些示例这些插入:

  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(HAS 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}});

This will return all documents with both a key called "IMAGE URL" and a non-null value.

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

Also, according to the docs, $exists currently can't use an index, but $ne can.

Edit: Adding some examples due to interest in this answer

Given these inserts:

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})

这篇关于如何在Mongo中查询这个? (不为null)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆