Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在MongoDB中测试未设置或缺少的字段_Javascript_Mongodb_Doctrine Odm - Fatal编程技术网

Javascript 在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}); 参考资料:

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

  • 还没定下来
  • 设置为空
  • …另外,一个数组是否包含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