gpt4 book ai didi

javascript - 测试 MongoDB 中未设置或缺失的字段

转载 作者:可可西里 更新时间:2023-11-01 09:08:29 24 4
gpt4 key购买 nike

在查询 MongoDB 数据库时是否有一种方法可以测试以下任一字段:

  • 尚未设置
  • 设置为空
  • ...作为奖励,数组是否包含 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

关于javascript - 测试 MongoDB 中未设置或缺失的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12190775/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com