MongoDB条件索引搜索字典中的数组

时间:2017-12-24 11:29:31

标签: mongodb

我刚开始使用MongoDB,我正在尝试按如下方式进行典型搜索。

查询问题:必须显示不提供特定菜肴的餐馆列表,其分数应为> 70和餐厅的纬度小于-65。

样本BSON:

[{
   "_id":ObjectId("5a3e3ea2c481f93a0aeccd83"),
   "address":{
      "building":"7715",
      "coord":[
         -73.9973325,
         40.61174889999999
      ],
      "street":"18 Avenue",
      "zipcode":"11214"
   },
   "borough":"Brooklyn",
   "cuisine":"American ",
   "grades":[
      {
         "date":         ISODate("2014-04-16T00:00:00Z"),
         "grade":"A",
         "score":5
      },
      {
         "date":         ISODate("2013-04-23T00:00:00Z"),
         "grade":"A",
         "score":2
      },
      {
         "date":         ISODate("2012-04-24T00:00:00Z"),
         "grade":"A",
         "score":5
      },
      {
         "date":         ISODate("2011-12-16T00:00:00Z"),
         "grade":"A",
         "score":2
      }
   ],
   "name":"C & C Catering Service",
   "restaurant_id":"40357437"
},
{
   "_id":ObjectId("5a3e3ea2c481f93a0aeccd84"),
   "address":{
      "building":"1269",
      "coord":[
         -73.871194,
         40.6730975
      ],
      "street":"Sutter Avenue",
      "zipcode":"11208"
   },
   "borough":"Brooklyn",
   "cuisine":"Chinese",
   "grades":[
      {
         "date":         ISODate("2014-09-16T00:00:00Z"),
         "grade":"B",
         "score":21
      },
      {
         "date":         ISODate("2013-08-28T00:00:00Z"),
         "grade":"A",
         "score":7
      },
      {
         "date":         ISODate("2013-04-02T00:00:00Z"),
         "grade":"C",
         "score":56
      },
      {
         "date":         ISODate("2012-08-15T00:00:00Z"),
         "grade":"B",
         "score":27
      },
      {
         "date":         ISODate("2012-03-28T00:00:00Z"),
         "grade":"B",
         "score":27
      }
   ],
   "name":"May May Kitchen",
   "restaurant_id":"40358429"
},
{
   "_id":ObjectId("5a3e3ea2c481f93a0aeccd85"),
   "address":{
      "building":"1",
      "coord":[
         -73.96926909999999,
         40.7685235
      ],
      "street":"East 66 Street",
      "zipcode":"10065"
   },
   "borough":"Manhattan",
   "cuisine":"American ",
   "grades":[
      {
         "date":         ISODate("2014-05-07T00:00:00Z"),
         "grade":"A",
         "score":3
      },
      {
         "date":         ISODate("2013-05-03T00:00:00Z"),
         "grade":"A",
         "score":4
      },
      {
         "date":         ISODate("2012-04-30T00:00:00Z"),
         "grade":"A",
         "score":6
      },
      {
         "date":         ISODate("2011-12-27T00:00:00Z"),
         "grade":"A",
         "score":0
      }
   ],
   "name":"1 East 66Th Street Kitchen",
   "restaurant_id":"40359480"
},
{
   "_id":ObjectId("5a3e3ea2c481f93a0aeccd86"),
   "address":{
      "building":"705",
      "coord":[
         -73.9653967,
         40.6064339
      ],
      "street":"Kings Highway",
      "zipcode":"11223"
   },
   "borough":"Brooklyn",
   "cuisine":"Jewish/Kosher",
   "grades":[
      {
         "date":         ISODate("2014-11-10T00:00:00Z"),
         "grade":"A",
         "score":11
      },
      {
         "date":         ISODate("2013-10-10T00:00:00Z"),
         "grade":"A",
         "score":13
      },
      {
         "date":         ISODate("2012-10-04T00:00:00Z"),
         "grade":"A",
         "score":7
      },
      {
         "date":         ISODate("2012-05-21T00:00:00Z"),
         "grade":"A",
         "score":9
      },
      {
         "date":         ISODate("2011-12-30T00:00:00Z"),
         "grade":"B",
         "score":19
      }
   ],
   "name":"Seuda Foods",
   "restaurant_id":"40360045"
},
{
   "_id":ObjectId("5a3e3ea2c481f93a0aeccd87"),
   "address":{
      "building":"203",
      "coord":[
         -73.97822040000001,
         40.6435254
      ],
      "street":"Church Avenue",
      "zipcode":"11218"
   },
   "borough":"Brooklyn",
   "cuisine":"Ice Cream, Gelato, Yogurt, Ices",
   "grades":[
      {
         "date":         ISODate("2014-02-10T00:00:00Z"),
         "grade":"A",
         "score":2
      },
      {
         "date":         ISODate("2013-01-02T00:00:00Z"),
         "grade":"A",
         "score":13
      },
      {
         "date":         ISODate("2012-01-09T00:00:00Z"),
         "grade":"A",
         "score":3
      },
      {
         "date":         ISODate("2011-11-07T00:00:00Z"),
         "grade":"P",
         "score":12
      },
      {
         "date":         ISODate("2011-07-21T00:00:00Z"),
         "grade":"A",
         "score":13
      }
   ],
   "name":"Carvel Ice Cream",
   "restaurant_id":"40360076"
},
{
   "_id":ObjectId("5a3e3ea2c481f93a0aeccd88"),
   "address":{
      "building":"265-15",
      "coord":[
         -73.7032601,
         40.7386417
      ],
      "street":"Hillside Avenue",
      "zipcode":"11004"
   },
   "borough":"Queens",
   "cuisine":"Ice Cream, Gelato, Yogurt, Ices",
   "grades":[
      {
         "date":         ISODate("2014-10-28T00:00:00Z"),
         "grade":"A",
         "score":9
      },
      {
         "date":         ISODate("2013-09-18T00:00:00Z"),
         "grade":"A",
         "score":10
      },
      {
         "date":         ISODate("2012-09-20T00:00:00Z"),
         "grade":"A",
         "score":13
      }
   ],
   "name":"Carvel Ice Cream",
   "restaurant_id":"40361322"
},
{
   "_id":ObjectId("5a3e3ea2c481f93a0aeccd89"),
   "address":{
      "building":"6909",
      "coord":[
         -74.0259567,
         40.6353674
      ],
      "street":"3 Avenue",
      "zipcode":"11209"
   },
   "borough":"Brooklyn",
   "cuisine":"Delicatessen",
   "grades":[
      {
         "date":         ISODate("2014-08-21T00:00:00Z"),
         "grade":"A",
         "score":4
      },
      {
         "date":         ISODate("2014-03-05T00:00:00Z"),
         "grade":"A",
         "score":3
      },
      {
         "date":         ISODate("2013-01-10T00:00:00Z"),
         "grade":"A",
         "score":10
      }
   ],
   "name":"Nordic Delicacies",
   "restaurant_id":"40361390"
}]

除了纬度小于-65之外,我几乎接近解决方案。请在下面找到我的问题。

db.addresses.find( { $and: [{"cuisine": { $not: /^A.*/ }}, {"grades.score":{ $gt: 70}}, {"address.coord":{ $lt: -65 } } ] } )

当我尝试以下查询时,它既不会抛出错误,也不会给我解决方案。

db.addresses.find( { $and: [{"cuisine": { $not: /^A.*/ }}, {"grades.score":{ $gt: 70}}, {"address.coord.1":{ $lt: -65 } } ] } )

请让我知道我在哪里犯了错误并纠正了我。

由于

1 个答案:

答案 0 :(得分:0)

您似乎正在查看address.coord的错误索引,请尝试以下操作:

db.addresses.find( { $and: [{"cuisine": { $not: /^A.*/ }}, {"grades.score":{ $gt: 70}}, {"address.coord.0":{ $lt: -65 } } ] } )