私はコレクションに3772ドキュメントがあり、score
の80 < score < 100
を持つgrades
配列の要素を含むすべてのドキュメントを検索しようとしています。
db.restaurants.find(
{"grades":
{$elemMatch: {score: {$gt: 80}, score: {$lt: 100}}}
}
)
戻り、すべてのドキュメント
db.restaurants.find(
{"grades":
{$elemMatch: {score: {$gt: 80, $lt: 100}}}
}
)
戻りますわずか3文書のに対し、:
しかし、私は次の2つのクエリの間には大きな乖離があることに気づきました。
$elemMatch
上documentationから、それは$ elemMatch演算子は、指定したすべてのクエリ条件に一致する少なくとも一つの要素を持つ配列フィールドを含む文書にマッチし
を述べています。
しかし、最初のクエリ($and
演算子のように動作する)が2番目のクエリとは異なる動作をしているように見えます。なぜ矛盾がありますか?
例の文書:あなたは{
と}
の1ペアでのMongoDBに渡す
{
"_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"
}