MongoDB Exercise - Find all the Chinese restaurants in Brooklyn
Write a MongoDB query to find all the Chinese restaurants in Brooklyn.
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({"borough": "Brooklyn", "cuisine": "Chinese"})
Output:
{ _id: ObjectId("6427d38d163a7d31d453f66b"), address: { building: '1269', coord: [ -73.871194, 40.6730975 ], street: 'Sutter Avenue', zipcode: '11208' }, borough: 'Brooklyn', cuisine: 'Chinese', grades: [ { date: ISODate("2014-09-16T00:00:00.000Z"), grade: 'B', score: 21 }, { date: ISODate("2013-08-28T00:00:00.000Z"), grade: 'A', score: 7 }, { date: ISODate("2013-04-02T00:00:00.000Z"), grade: 'C', score: 56 }, { date: ISODate("2012-08-15T00:00:00.000Z"), grade: 'B', score: 27 }, { date: ISODate("2012-03-28T00:00:00.000Z"), grade: 'B', score: 27 } ], name: 'May May Kitchen', restaurant_id: '40358429' }, { _id: ObjectId("6427d38d163a7d31d453f693"), address: { building: '976', coord: [ -73.92701509999999, 40.6620192 ], street: 'Rutland Road', zipcode: '11212' }, borough: 'Brooklyn', cuisine: 'Chinese', grades: [ { date: ISODate("2014-04-23T00:00:00.000Z"), grade: 'A', score: 13 }, { date: ISODate("2013-03-26T00:00:00.000Z"), grade: 'A', score: 10 }, { date: ISODate("2012-03-13T00:00:00.000Z"), grade: 'A', score: 4 }, { date: ISODate("2011-11-16T00:00:00.000Z"), grade: 'A', score: 13 } ], name: 'Golden Pavillion', restaurant_id: '40363920' }, .....
Explanation:
The said query in MongoDB that returns all the documents in the restaurants collection that have a borough with value "Brooklyn" and a cuisine value of "Chinese".
The find method is called on the restaurants collection, and the query condition is passed as an argument in the form of a JSON object.
Note: This output is generated using MongoDB server version 3.6
Improve this sample solution and post your code through Disqus.
Previous: Find the restaurants with the highest total score.
Next: Find the restaurant with the most recent grade date.
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://w3resource.com/mongodb-exercises/mongodb-exercise-83.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics