MongoDB Exercise - Display specific fields of a document
Write a MongoDB query to display the fields restaurant_id, name, borough and cuisine for all the documents in the collection restaurant.
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({},{"restaurant_id" : 1,"name":1,"borough":1,"cuisine" :1});
Output:
{ "_id" : ObjectId("564c2d939eb21ad392f175c9"), "borough" : "Manhattan", "cuisine" : "Irish", "name" : "Dj Reynolds Pub And Restaurant", "restaurant_id" : "30191841" } { "_id" : ObjectId("564c2d939eb21ad392f175ca"), "borough" : "Bronx", "cuisine" : "Bakery", "name" : "Morris Park Bake Shop", "restaurant_id" : "30075445" } { "_id" : ObjectId("564c2d939eb21ad392f175cb"), "borough" : "Brooklyn", "cuisine" : "American ", "name" : "Riviera Caterer", "restaurant_id" : "40356018" } { "_id" : ObjectId("564c2d939eb21ad392f175cc"), "borough" : "Brooklyn", "cuisine" : "Hamburgers", "name" : "Wendy'S", "restaurant_id" : "30112340" } { "_id" : ObjectId("564c2d939eb21ad392f175cd"), "borough" : "Queens", "cuisine" : "Jewish/Kosher", "name" : "Tov Kosher Kitchen", "restaurant_id" : "40356068" } { "_id" : ObjectId("564c2d939eb21ad392f175ce"), "borough" : "Queens", "cuisine" : "American ", "name" : "Brunos On The Boulevard", "restaurant_id" : "40356151" } { "_id" : ObjectId("564c2d939eb21ad392f175cf"), "borough" : "Brooklyn", "cuisine" : "American ", "name" : "Regina Caterers", "restaurant_id" : "40356649" } { "_id" : ObjectId("564c2d939eb21ad392f175d0"), "borough" : "Brooklyn", "cuisine" : "Delicatessen", "name" : "Wilken'S Fine Food", "restaurant_id" : "40356483" } { "_id" : ObjectId("564c2d939eb21ad392f175d1"), "borough" : "Bronx", "cuisine" : "American ", "name" : "Wild Asia", "restaurant_id" : "40357217" } { "_id" : ObjectId("564c2d939eb21ad392f175d2"), "borough" : "Brooklyn", "cuisine" : "Ice Cream, Gelato, Yogurt, Ices", "name" : "Taste The Tropics Ice Cream", "restaurant_id" : "40356731" } { "_id" : ObjectId("564c2d939eb21ad392f175d3"), "borough" : "Brooklyn", "cuisine" : "American ", "name" : "C & C Catering Service", "restaurant_id" : "40357437" } { "_id" : ObjectId("564c2d939eb21ad392f175d4"), "borough" : "Brooklyn", "cuisine" : "Chinese", "name" : "May May Kitchen", "restaurant_id" : "40358429" } { "_id" : ObjectId("564c2d939eb21ad392f175d5"), "borough" : "Manhattan", "cuisine" : "American ", "name" : "1 East 66Th Street Kitchen", "restaurant_id" : "40359480" } { "_id" : ObjectId("564c2d939eb21ad392f175d6"), "borough" : "Brooklyn", "cuisine" : "Jewish/Kosher", "name" : "Seuda Foods", "restaurant_id" : "40360045" } { "_id" : ObjectId("564c2d949eb21ad392f1c593"), "borough" : "Queens", "cuisine" : "Other", "name" : "Laquana King", "restaurant_id" : "50003441" } { "_id" : ObjectId("564c2d939eb21ad392f175d7"), "borough" : "Brooklyn", "cuisine" : "Ice Cream, Gelato, Yogurt, Ices", "name" : "Carvel Ice Cream", "restaurant_id" : "40360076" } { "_id" : ObjectId("564c2d939eb21ad392f175d8"), "borough" : "Queens", "cuisine" : "Ice Cream, Gelato, Yogurt, Ices", "name" : "Carvel Ice Cream", "restaurant_id" : "40361322" } { "_id" : ObjectId("564c2d939eb21ad392f175d9"), "borough" : "Brooklyn", "cuisine" : "Delicatessen", "name" : "Nordic Delicacies", "restaurant_id" : "40361390" } { "_id" : ObjectId("564c2d939eb21ad392f175da"), "borough" : "Brooklyn", "cuisine" : "American ", "name" : "The Movable Feast", "restaurant_id" : "40361606" } { "_id" : ObjectId("564c2d939eb21ad392f175db"), "borough" : "Manhattan", "cuisine" : "American ", "name" : "Glorious Food", "restaurant_id" : "40361521" } Type "it" for more
Note: This output is generated using MongoDB server version 3.6
Explanation:
The said MongoDB shell command that retrieves data from the 'restaurants' collection in the current database.
An empty object {} as the first argument, which acts as a filter to retrieve all documents in the collection.
A second object as the second argument that specifies the fields "restaurant_id", "name", "borough", and "cuisine" to include in the results.
The command would return a cursor to the documents that match the filter, including only the specified fields.
Improve this sample solution and post your code through Disqus.
Previous: Find all the documents in the collection restaurants.
Next: Select restaurant_id, name, borough, and cuisine, excluding the field _id.
What is the difficulty level of this exercise?
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics