2016-07-28 2 views
0

「grades.score」:{$ lte:10}を使用すると、10を超える値のスコアも成績配列に表示されます。しかし、 "grades.score":{$ not:{$ gt:10}}を使用すると、それらは除外されます。 両方の意味は同じであるように見えますが。ここで

コレクションすなわち "レストラン" のサンプルです:ここで

{ 
    "address": { 
    "building": "1007", 
    "coord": [ -73.856077, 40.848447 ], 
    "street": "Morris Park Ave", 
    "zipcode": "10462" 
    }, 
    "borough": "Bronx", 
    "cuisine": "Bakery", 
    "grades": [ 
    { "grade": "A", "score": 2 }, 
    { "grade": "A", "score": 6 }, 
    { "grade": "A", "score": 10 }, 
    { "grade": "A", "score": 9 }, 
    { "grade": "B", "score": 14 } 
    ], 
    "name": "Morris Park Bake Shop", 
    "restaurant_id": "30075445" 
} 

は、第一クエリです:

db.restaurants.find({"grades.score":{$lte:10}},{"grades.score":1,name:1,restau rant_id:1,borough:1,cuisine:1,_id:0}).pretty()

結果:

{ 
    "borough" : "Queens", 
    "cuisine" : "Ice Cream, Gelato, Yogurt, Ices", 
    "grades" : [ 
      { 
        "score" : 9 
      }, 
      { 
        "score" : 10 
      }, 
      { 
        "score" : 13 
      } 
    ], 
    "name" : "Carvel Ice Cream", 
    "restaurant_id" : "40361322" 
} 
{ 
    "borough" : "Brooklyn", 
    "cuisine" : "Delicatessen", 
    "grades" : [ 
      { 
        "score" : 4 
      }, 
      { 
        "score" : 3 
      }, 
      { 
        "score" : 10 
      } 
    ], 
    "name" : "Nordic Delicacies", 
    "restaurant_id" : "40361390" 
} 
{ 
    "borough" : "Manhattan", 
    "cuisine" : "American ", 
    "grades" : [ 
      { 
        "score" : 12 
      }, 
      { 
        "score" : 16 
      }, 
      { 
        "score" : 9 
      }, 
      { 
        "score" : 13 
      }, 
      { 
        "score" : 11 
      } 
    ], 
    "name" : "Glorious Food", 
    "restaurant_id" : "40361521" 
} 
{ 
    "borough" : "Brooklyn", 
    "cuisine" : "American ", 
    "grades" : [ 
      { 
        "score" : 11 
      }, 
      { 
        "score" : 2 
      }, 
      { 
        "score" : 13 
      }, 
      { 
        "score" : 11 
      } 
    ], 
    "name" : "The Movable Feast", 
    "restaurant_id" : "40361606" 
} 
{ 
    "borough" : "Queens", 
    "cuisine" : "Delicatessen", 
    "grades" : [ 
      { 
        "score" : 12 
      }, 
      { 
        "score" : 9 
      }, 
      { 
        "score" : 7 
      }, 
      { 
        "score" : 10 
      } 
    ], 
    "name" : "Sal'S Deli", 
    "restaurant_id" : "40361618" 
} 

ここでは、第二のクエリです:

db.restaurants.find({"grades.score":{$not:{$gt:10}}}, {"grades.score":1,name:1,restaurant_id:1,borough:1,cuisine:1,_id:0}).pretty()

結果:

{ 
    "borough" : "Bronx", 
    "cuisine" : "American ", 
    "grades" : [ 
      { 
        "score" : 5 
      }, 
      { 
        "score" : 3 
      }, 
      { 
        "score" : 4 
      }, 
      { 
        "score" : 9 
      } 
    ], 
    "name" : "African Market (Baboon Cafe)", 
    "restaurant_id" : "40368026" 
} 
{ 
    "borough" : "Staten Island", 
    "cuisine" : "Italian", 
    "grades" : [ 
      { 
        "score" : 10 
      }, 
      { 
        "score" : 3 
      }, 
      { 
        "score" : 6 
      }, 
      { 
        "score" : 10 
      } 
    ], 
    "name" : "Roadhouse Restaurant", 
    "restaurant_id" : "40368034" 
} 
{ 
    "borough" : "Manhattan", 
    "cuisine" : "French", 
    "grades" : [ 
      { 
        "score" : 9 
      }, 
      { 
        "score" : 2 
      }, 
      { 
        "score" : 8 
      } 
    ], 
    "name" : "Pergola Des Artistes", 
    "restaurant_id" : "40369139" 
} 
{ 
    "borough" : "Brooklyn", 
    "cuisine" : "Hamburgers", 
    "grades" : [ 
      { 
        "score" : 10 
      }, 
      { 
        "score" : 7 
      }, 
      { 
        "score" : 5 
      }, 
      { 
        "score" : 10 
      } 
    ], 
    "name" : "Mcdonald'S", 
    "restaurant_id" : "40369535" 
} 
{ 
    "borough" : "Brooklyn", 
    "cuisine" : "American ", 
    "grades" : [ 
      { 
        "score" : 10 
      }, 
      { 
        "score" : 8 
      } 
    ], 
    "name" : "The River Cafe", 
    "restaurant_id" : "40369608" 
} 

答えて

0

クエリのどちらが実際にgrades配列の外にアイテムをフィルタリングします。

Query a Field that Contains an Array

フィールドが配列が含まれており、クエリが複数の条件演算子を持っている場合のいずれか一つの配列要素が条件または配列の組み合わせを満たしている場合、全体としてフィールドが一致します要素が条件を満たす。 10.

あなた2クエリ以下score値を有するgradesアレイ内の少なくとも1つの項目を含む

あなたの第1クエリ、

db.restaurants.find({"grades.score":{$lte:10}},{"grades.score":1,name:1,restaurant_id:1,borough:1, cuisine:1,_id:0}).pretty() 

戻る文書、

db.restaurants.find({"grades.score":{$not:{$gt:10}}},{"grades.score":1,name:1,restaurant_id:1,borough:1,cuisine:1,_id:0}).pretty() 

返信doc grades配列内にscoreの値が10より大きい項目が含まれていません。

私があなたが意図していることは、このSO answerに記載されています。

+0

答えはThansですが、どちらのクエリでも正確に等しい結果が得られるはずです。だから彼らはどうやって異なる結果を出しているのですか? – shashi

+0

すべての文書が 'grades'配列内に単一の項目を持っていた場合、同じ結果が得られます。 '.find({" grades.score ":...})'は、配列内の少なくとも1つの要素が条件に一致する場合、配列のすべての項目を返します。 – Kyriakos

関連する問題