【问题标题】:MongoDB condition index searching of an array inside a dictionary字典内数组的MongoDB条件索引搜索
【发布时间】:2017-12-24 11:29:30
【问题描述】:

我是 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 } } ] } )

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

谢谢

【问题讨论】:

    标签: mongodb


    【解决方案1】:

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

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

    【讨论】:

    • 非常感谢stackoverflow.com/users/4079967/kevin-smith 很抱歉你是个笨蛋,直截了当地粘贴数据集
    • 没问题,我们都会犯错误
    • 是的,我接受了它并竖起了大拇指。但它没有反映。
    猜你喜欢
    • 2011-09-05
    • 2016-02-02
    • 2020-11-21
    • 1970-01-01
    • 2016-11-23
    • 1970-01-01
    • 2020-07-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多