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("564c2d939eb21ad392f175d3"), "borough" : "Brooklyn", "cuisine" : "American ", "name" : "C & C Catering Service", "restaurant_id" : "40357437" } { "_id" : ObjectId("564c2d939eb21ad392f175d5"), "borough" : "Manhattan", "cuisine" : "American ", "name" : "1 East 66Th Street Kitchen", "restaurant_id" : "40359480" } { "_id" : ObjectId("564c2d949eb21ad392f1c593"), "borough" : "Queens", "cuisine" : "Other", "name" : "Laquana King", "restaurant_id" : "50003441" } { "_id" : ObjectId("564c2d939eb21ad392f175d9"), "borough" : "Brooklyn", "cuisine" : "Delicatessen", "name" : "Nordic Delicacies", "restaurant_id" : "40361390" } { "_id" : ObjectId("564c2d939eb21ad392f175e6"), "borough" : "Brooklyn", "cuisine" : "Hamburgers", "name" : "White Castle", "restaurant_id" : "40362344" } { "_id" : ObjectId("564c2d939eb21ad392f175f4"), "borough" : "Brooklyn", "cuisine" : "American ", "name" : "Sonny'S Heros", "restaurant_id" : "40363744" } { "_id" : ObjectId("564c2d939eb21ad392f17605"), "borough" : "Bronx", "cuisine" : "American ", "name" : "Manhem Club", "restaurant_id" : "40364363" } { "_id" : ObjectId("564c2d949eb21ad392f1c54d"), "borough" : "Queens", "cuisine" : "Other", "name" : "Zoe Place", "restaurant_id" : "50003255" } { "_id" : ObjectId("564c2d939eb21ad392f17613"), "borough" : "Staten Island", "cuisine" : "American ", "name" : "Great Kills Yacht Club", "restaurant_id" : "40364610" } { "_id" : ObjectId("564c2d939eb21ad392f1761a"), "borough" : "Manhattan", "cuisine" : "American ", "name" : "Serendipity 3", "restaurant_id" : "40364863" } { "_id" : ObjectId("564c2d939eb21ad392f1761f"), "borough" : "Manhattan", "cuisine" : "American ", "name" : "White Horse Tavern", "restaurant_id" : "40364958" } { "_id" : ObjectId("564c2d939eb21ad392f17627"), "borough" : "Manhattan", "cuisine" : "Irish", "name" : "Dorrian'S Red Hand Restaurant", "restaurant_id" : "40365239" } { "_id" : ObjectId("564c2d939eb21ad392f17678"), "borough" : "Manhattan", "cuisine" : "Mexican", "name" : "Mexico Lindo Restaurant", "restaurant_id" : "40367038" } { "_id" : ObjectId("564c2d939eb21ad392f17699"), "borough" : "Brooklyn", "cuisine" : "Greek", "name" : "El Greco Diner", "restaurant_id" : "40367795" } { "_id" : ObjectId("564c2d939eb21ad392f1769d"), "borough" : "Bronx", "cuisine" : "Not Listed/Not Applicable", "name" : "The Lark'S Nest", "restaurant_id" : "40367946" } { "_id" : ObjectId("564c2d939eb21ad392f1769e"), "borough" : "Bronx", "cuisine" : "African", "name" : "African Terrace", "restaurant_id" : "40368021" } { "_id" : ObjectId("564c2d939eb21ad392f1769f"), "borough" : "Bronx", "cuisine" : "Café/Coffee/Tea", "name" : "Terrace Cafe", "restaurant_id" : "40368018" } { "_id" : ObjectId("564c2d939eb21ad392f176a3"), "borough" : "Bronx", "cuisine" : "American ", "name" : "African Market (Baboon Cafe)", "restaurant_id" : "40368026" } { "_id" : ObjectId("564c2d939eb21ad392f176a4"), "borough" : "Staten Island", "cuisine" : "Italian", "name" : "Roadhouse Restaurant", "restaurant_id" : "40368034" } { "_id" : ObjectId("564c2d939eb21ad392f176c6"), "borough" : "Manhattan", "cuisine" : "French", "name" : "Pergola Des Artistes", "restaurant_id" : "40369139" } Type "it" for more
Note: This output is generated using MongoDB server version 3.6
Explanation:
The said query in MongoDB that retrieves the fields "restaurant_id", "name", "borough", and "cuisine" from the 'restaurants' collection .
The condition specified as the $gt operator, which first filters the "grades.score" have a value greater than 10 and the $not operator is used to negate the condition that is excludes those documents.
Improve this sample solution and post your code through Disqus.
Previous: Identify restaurants not in Staten Island, Queens, Bronx, or Brooklyn by their restaurant id, name, and borough.
Next: Find the Id, name, borough, and cuisine of restaurants that don't prepare American or Chinese dishes or whose names begin with Wil.
What is the difficulty level of this exercise?
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/mongodb-exercises/mongodb-exercise-20.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics