Javascript 在MongoDB中测试未设置或缺少的字段
在查询MongoDB数据库时,是否有方法测试以下字段:Javascript 在MongoDB中测试未设置或缺少的字段,javascript,mongodb,doctrine-odm,Javascript,Mongodb,Doctrine Odm,在查询MongoDB数据库时,是否有方法测试以下字段: 还没定下来 设置为空 …另外,一个数组是否包含null作为其值之一 操作员检查字段是否已设置 要检查字段的值是否为空,可以直接编写查找查询 假设coll代表集合名称,field代表字段名称(添加字段“n”以区分): 更新:Leftium确实正确;您只需要db.coll.find({field:null})。更新我的答案以反映这一点。这应该涵盖您的所有三种情况: db.FOO.find({BAR: null}); 参考资料:
- 还没定下来
- 设置为空
- …另外,一个数组是否包含null作为其值之一
假设coll代表集合名称,field代表字段名称(添加字段“n”以区分):
更新:Leftium确实正确;您只需要
db.coll.find({field:null})代码>。更新我的答案以反映这一点。这应该涵盖您的所有三种情况:
db.FOO.find({BAR: null});
参考资料:
您可以从Mongo shell验证:
> db.foo.drop();
true
> db.foo.insert({_id:1, bar:1, matches:'NO'});
> db.foo.insert({_id:2, matches:'YES'});
> db.foo.insert({_id:3, bar:null, matches:'YES'});
> db.foo.insert({_id:4, bar:[1,2,3], matches:'NO'});
> db.foo.insert({_id:5, bar:[1,2,null], matches:'YES'});
>
> db.foo.find({bar: null});
{ "_id" : 2, "matches" : "YES" }
{ "_id" : 3, "bar" : null, "matches" : "YES" }
{ "_id" : 5, "bar" : [ 1, 2, null ], "matches" : "YES" }
> db.foo.count({bar: null});
3
> db.foo.count({matches: 'YES'});
3
> db.foo.drop();
true
> db.foo.insert({_id:1, bar:1, matches:'NO'});
> db.foo.insert({_id:2, matches:'YES'});
> db.foo.insert({_id:3, bar:null, matches:'YES'});
> db.foo.insert({_id:4, bar:[1,2,3], matches:'NO'});
> db.foo.insert({_id:5, bar:[1,2,null], matches:'YES'});
>
> db.foo.find({bar: null});
{ "_id" : 2, "matches" : "YES" }
{ "_id" : 3, "bar" : null, "matches" : "YES" }
{ "_id" : 5, "bar" : [ 1, 2, null ], "matches" : "YES" }
> db.foo.count({bar: null});
3
> db.foo.count({matches: 'YES'});
3