Reputation: 11
I am nascent to the MongoDB and I am trying to do a typical search as follows.
Query problem: Have to display the list of restaurants which does not serve a certain cuisine AND their score should be > 70 AND the restaurant latitude is less than -65.
Sample 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"
}]
I'm almost near to the solution except the latitude less than -65. Please find my query below.
db.addresses.find( { $and: [{"cuisine": { $not: /^A.*/ }}, {"grades.score":{ $gt: 70}}, {"address.coord":{ $lt: -65 } } ] } )
When I tried the following query, it neither throws an error, nor gives me the solution.
db.addresses.find( { $and: [{"cuisine": { $not: /^A.*/ }}, {"grades.score":{ $gt: 70}}, {"address.coord.1":{ $lt: -65 } } ] } )
Please let me know where am I making the mistake and correct me.
Thanks
Upvotes: 1
Views: 203
Reputation: 14436
Looks like you're looking at the wrong index for address.coord
, try the following:
db.addresses.find( { $and: [{"cuisine": { $not: /^A.*/ }}, {"grades.score":{ $gt: 70}}, {"address.coord.0":{ $lt: -65 } } ] } )
Upvotes: 1