2016-07-28 47 views
0

我们可以看到,当使用“grades.score”:{$ lte:10}时,大于10的值也会出现在grades数组中。但是当使用“scores.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

如果字段中包含的阵列,并且查询具有多个条件运算符,就好像是单个阵列元件满足条件或阵列的组合的整个将匹配字段元素符合条件。

你的第一查询时,

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

返回包含grades阵列,其具有小于或等于10。

你的第二查询score值中的至少一个项目的文件,

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

return doc在grades阵列中没有任何项目,其score值大于10.

我假设您打算这么做,在此SO answer中进行了描述。

+0

Thans回答,但我认为这两个查询应给出完全相同的结果。那么他们如何给出不同的结果呢? – shashi

+0

如果所有文档在'grades'数组中有单个项目,那么您将得到相同的结果。 '.find({“grades.score”:...})如果该数组中至少有一个元素与该条件匹配,则返回该数组的所有项。 – Kyriakos

相关问题