w3resource
w3resource logo

MongoDB Exercise - Find the restaurant Id, name, borough and cuisine for those restaurants which achieved a score which is not more than 10


Write a MongoDB query to find the restaurant Id, name, borough and cuisine for those restaurants which achieved a score which is not more than 10.

Structure of 'restaurants' collection :

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

Query:

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

Output:

{ "_id" : ObjectId("5ab23ff163b071e80805c4cf"), "borough" : "Brooklyn", "cuisine" : "American", "name" : "C & C Catering Service", "restaurant_id" : "40357437" }
{ "_id" : ObjectId("5ab23ff163b071e80805c4d1"), "borough" : "Manhattan", "cuisine" : "American", "name" : "1 East 66Th Street Kitchen", "restaurant_id" : "40359480" }
{ "_id" : ObjectId("5ab23ff163b071e80805c4d5"), "borough" : "Brooklyn", "cuisine" : "Delicatessen", "name" : "Nordic Delicacies", "restaurant_id" : "40361390" }
{ "_id" : ObjectId("5ab23ff163b071e80805c4dd"), "borough" : "Brooklyn", "cuisine" : "Hamburgers", "name" : "White Castle", "restaurant_id" : "40362344" }
{ "_id" : ObjectId("5ab23ff163b071e80805c4f0"), "borough" : "Brooklyn", "cuisine" : "American", "name" : "Sonny'S Heros", "restaurant_id" : "40363744" }
{ "_id" : ObjectId("5ab23ff163b071e80805c501"), "borough" : "Bronx", "cuisine" : "American", "name" : "Manhem Club", "restaurant_id" : "40364363" }
{ "_id" : ObjectId("5ab23ff163b071e80805c50e"), "borough" : "Staten Island", "cuisine" : "American", "name" : "Great Kills Yacht Club", "restaurant_id" : "40364610" }
{ "_id" : ObjectId("5ab23ff163b071e80805c516"), "borough" : "Manhattan", "cuisine" : "American", "name" : "Serendipity 3", "restaurant_id" : "40364863" }
{ "_id" : ObjectId("5ab23ff163b071e80805c51a"), "borough" : "Manhattan", "cuisine" : "American", "name" : "White Horse Tavern", "restaurant_id" : "40364958" }
{ "_id" : ObjectId("5ab23ff163b071e80805c523"), "borough" : "Manhattan", "cuisine" : "Irish", "name" : "Dorrian'S Red Hand Restaurant", "restaurant_id" : "40365239" }
{ "_id" : ObjectId("5ab23ff163b071e80805c573"), "borough" : "Manhattan", "cuisine" : "Mexican", "name" : "Mexico Lindo Restaurant", "restaurant_id" : "40367038" }
{ "_id" : ObjectId("5ab23ff163b071e80805c594"), "borough" : "Brooklyn", "cuisine" : "Greek", "name" : "El Greco Diner", "restaurant_id" : "40367795" }
{ "_id" : ObjectId("5ab23ff163b071e80805c59a"), "borough" : "Bronx", "cuisine" : "Not Listed/Not Applicable", "name" : "The Lark'S Nest", "restaurant_id" : "40367946" }
{ "_id" : ObjectId("5ab23ff163b071e80805c59b"), "borough" : "Bronx", "cuisine" : "African", "name" : "African Terrace", "restaurant_id" : "40368021" }
{ "_id" : ObjectId("5ab23ff163b071e80805c59c"), "borough" : "Bronx", "cuisine" : "Café/Coffee/Tea", "name" : "Terrace Cafe", "restaurant_id" : "40368018" }
{ "_id" : ObjectId("5ab23ff163b071e80805c59f"), "borough" : "Bronx", "cuisine" : "American", "name" : "African Market (Baboon Cafe)", "restaurant_id" : "40368026" }
{ "_id" : ObjectId("5ab23ff163b071e80805c5a0"), "borough" : "Staten Island", "cuisine" : "Italian", "name" : "Roadhouse Restaurant", "restaurant_id" : "40368034" }
{ "_id" : ObjectId("5ab23ff163b071e80805c5c2"), "borough" : "Manhattan", "cuisine" : "French", "name" : "Pergola Des Artistes", "restaurant_id" : "40369139" }
{ "_id" : ObjectId("5ab23ff163b071e80805c5cd"), "borough" : "Brooklyn", "cuisine" : "Hamburgers", "name" : "Mcdonald'S", "restaurant_id" : "40369535" }
{ "_id" : ObjectId("5ab23ff163b071e80805c5d5"), "borough" : "Brooklyn", "cuisine" : "American", "name" : "The River Cafe", "restaurant_id" : "40369608" }
Type "it" for more

Note: This output is generated using MongoDB server version 3.6

Improve this sample solution and post your code through Disqus.