我有一个包含3772个文档的 restaurants 集合,我试图找到包含 grades 数组中 score 为 80 < score < 100 的元素的所有文档 .
但是,我注意到以下两个查询之间存在很大差异:
db.restaurants.find(
{"grades":
{$elemMatch: {score: {$gt: 80}, score: {$lt: 100}}}
}
)
返回所有文档,而
db.restaurants.find(
{"grades":
{$elemMatch: {score: {$gt: 80, $lt: 100}}}
}
)
只返回3个文件 .
从documentation开始 $elemMatch ,它说明了
$ elemMatch运算符匹配包含数组字段的文档,其中至少有一个元素匹配所有指定的查询条件 .
但是,我注意到第一个查询(类似于 $and 运算符)似乎与第二个查询的执行方式不同 . 为什么会出现差异?
Example document:
{
"_id" : ObjectId("57290430139a4a37132ca096"),
"address" : {
"building" : "345",
"coord" : [
-73.9864626,
40.7266739
],
"street" : "East 6 Street",
"zipcode" : "10003"
},
"borough" : "Manhattan",
"cuisine" : "Indian",
"grades" : [
{
"date" : ISODate("2013-05-30T00:00:00Z"),
"grade" : "A",
"score" : 12
},
{
"date" : ISODate("2012-04-06T00:00:00Z"),
"grade" : "C",
"score" : 92
},
{
"date" : ISODate("2011-11-03T00:00:00Z"),
"grade" : "C",
"score" : 41
}
],
"restaurant_id" : "40381295"
}