Mongodb Mongo DB在隐式和

Mongodb Mongo DB在隐式和,mongodb,mongodb-query,nosql,Mongodb,Mongodb Query,Nosql,我是mongodb的新手,最近开始学习基本语法。我用find方法尝试运算符,在尝试隐式和隐式时遇到了一个令人困惑的情况 我收集的mathtable有400个文档如下: { "_id" : ObjectId("540efc2bd8af78d9b0f5d4b2") , "index" : 1 } { "_id" : ObjectId("540efc2bd8af78d9b0f5d4b3") , "index" : 2 } { "_id" : ObjectId("540efc2bd8af78

我是mongodb的新手,最近开始学习基本语法。我用find方法尝试运算符,在尝试隐式和隐式时遇到了一个令人困惑的情况

我收集的
mathtable
400个文档如下:

{ "_id" : ObjectId("540efc2bd8af78d9b0f5d4b2")  , "index" : 1   }
{ "_id" : ObjectId("540efc2bd8af78d9b0f5d4b3")  , "index" : 2   }
{ "_id" : ObjectId("540efc2bd8af78d9b0f5d4b4")  , "index" : 3   }
{ "_id" : ObjectId("540efc2bd8af78d9b0f5d4b5")  , "index" : 4   }
{ "_id" : ObjectId("540efc2bd8af78d9b0f5d4b6")  , "index" : 5   }
{ "_id" : ObjectId("540efc2bd8af78d9b0f5d4b7")  , "index" : 6   }
{ "_id" : ObjectId("540efc2bd8af78d9b0f5d4b8")  , "index" : 7   }
{ "_id" : ObjectId("540efc2bd8af78d9b0f5d4b9")  , "index" : 8   }
{ "_id" : ObjectId("540efc2bd8af78d9b0f5d4ba")  , "index" : 9   }
{ "_id" : ObjectId("540efc2bd8af78d9b0f5d4bb")  , "index" : 10  }
{ "_id" : ObjectId("540efc2bd8af78d9b0f5d4bc")  , "index" : 11  }
{ "_id" : ObjectId("540efc2bd8af78d9b0f5d4bd")  , "index" : 12  }
{ "_id" : ObjectId("540efc2bd8af78d9b0f5d4be")  , "index" : 13  }
{ "_id" : ObjectId("540efc2bd8af78d9b0f5d4bf")  , "index" : 14  }
{ "_id" : ObjectId("540efc2bd8af78d9b0f5d4c0")  , "index" : 15  }
{ "_id" : ObjectId("540efc2bd8af78d9b0f5d4c1")  , "index" : 16  }
{ "_id" : ObjectId("540efc2bd8af78d9b0f5d4c2")  , "index" : 17  }
{ "_id" : ObjectId("540efc2bd8af78d9b0f5d4c3")  , "index" : 18  }
{ "_id" : ObjectId("540efc2bd8af78d9b0f5d4c4")  , "index" : 19  }
{ "_id" : ObjectId("540efc2bd8af78d9b0f5d4c5")  , "index" : 20  }
{ "_id" : ObjectId("540efc2bd8af78d9b0f5d4d1")  , "index" : 1   }
..
..
{ "_id" : ObjectId("540efc2bd8af78d9b0f5d4z5")  , "index" : 20 }
mathtable
集合中有400行:

  • 索引的值范围为
    1到20
  • 对于
    索引
    的每个值,都有
    20个
    条目具有不同的id值
考虑到它们都是
隐式和
情况,我尝试下面两个操作并期望得到相同的结果

计算偶数
索引
值大于5的值。

  • 使用经典显式AND(将结果转换为160条记录):

  • 仅使用变量名一次(结果为160条记录):

  • 在每个条件下使用字段名(结果为300条记录):

  • 将字段名与每个条件一起使用,条件按相反顺序排列(生成200条记录):

mongoDB文档中没有提到
隐式和
(或者至少我没有找到像
隐式和
这样的直接引用)

我希望在这两种情况下都有相同数量的记录(
160
)。我无法理解上述代码为何表现不同

此外,条件规范的顺序会产生不同数量的结果。根据观察,当多次指定同一字段时,仅应用find中指定的最后一个条件这既奇怪又不正确。


注意:我使用的是
Mongo-DB-2.6
,代码是在发行版附带的
Mongo shell
上执行的。

Json或关联数组或映射
不包含重复键:

db.mathtable.find({ 
                  index : { $mod : [2,0]} , 
                  index : {$gt:5} 
                 });
上述内容将被视为等同于:

db.mathtable.find({ 
                  index : {$gt:5} 
                 });
第一个条件将被覆盖

以下是

db.mathtable.find({ 
                  index :  {$gt:5} , 
                  index : { $mod : [2,0]}
                 });
相当于,

db.mathtable.find({ 
                  index : { $mod : [2,0]}
                 });
但在第一种情况下,

db.mathtable.count({ 
            $and: [
            { index: { $mod: [2,0] } },         
            { index: { $gt: 5 } }     
            ] 
        });
$and将两个json文档作为输入,并按预期运行

db.mathtable.count({ 
                  index : { $mod : [2,0] ,  $gt:5 } 
                  });
在第二种情况下,count获取一个没有重复键的文档,并按照预期的方式运行

db.mathtable.count({ 
                  index : { $mod : [2,0] ,  $gt:5 } 
                  });

因此返回的行数不同。希望对你有帮助

这对理解原因很有帮助。
db.mathtable.find({ 
                  index : { $mod : [2,0]}
                 });
db.mathtable.count({ 
            $and: [
            { index: { $mod: [2,0] } },         
            { index: { $gt: 5 } }     
            ] 
        });
db.mathtable.count({ 
                  index : { $mod : [2,0] ,  $gt:5 } 
                  });