Skip to content

Instantly share code, notes, and snippets.

@bhawna94
Last active August 13, 2020 07:42
Show Gist options
  • Save bhawna94/6963e23edaf7c4b6371decdb0118fe07 to your computer and use it in GitHub Desktop.
Save bhawna94/6963e23edaf7c4b6371decdb0118fe07 to your computer and use it in GitHub Desktop.
1. Write a MongoDB query to display all the documents in the collection restaurants.
db.restaurants.find().pretty()
or
db.restaurants.find()
2. Write a MongoDB query to display the fields restaurant_id, name, borough and cuisine for all the documents in the collection restaurant
db.restaurants.find({}, {"restaurant_id":1, "name":1,"borough":1 , "cuisine":1})
3. Write a MongoDB query to display the fields restaurant_id, name, borough and cuisine, but exclude the field _id for all the documents in the collection restaurant.
db.restaurants.find({}, {"restaurant_id":1, "name":1,"borough":1 , "cuisine":1, "_id":0})
4. Write a MongoDB query to display all the restaurant which is in the borough Bronx.
db.restaurants.find({"borough": "Bronx"}).pretty()
5.Write a MongoDB query to display the first 5 restaurant which is in the borough Bronx
db.restaurants.find({"borough": "Bronx"}).limit(5)
6.Write a MongoDB query to display the next 5 restaurants after skipping first 5 which are in the borough Bronx
db.restaurants.find({"borough": "Bronx"}).skip(5).limit(5)
7.Write a MongoDB query to find the restaurants who achieved a score more than 90
db.restaurants.find({"grades.score":{$gt:90}}).pretty()
8.Write a MongoDB query to find the restaurants that achieved a score, more than 80 but less than 100.
db.restaurants.find({"grades.score":{$gt:80,$lt:100}}).pretty()
9.Write a MongoDB query to find the restaurants which locate in latitude value less than -95.754168
db.restaurants.find({"address.coord.0":{$lt:-95.754168}}).pretty()
10. Write a MongoDB query to find the restaurants that do not prepare any cuisine of 'American' and their grade score more than 70 and latitude less than -65.754168
db.restaurants.find({$and:[{"cuisine" : {$ne :"American"}},{"grades.score":{$gt:70}},{"address.coord.0":{$lt:-65.754168}}]});
11. Write a MongoDB query to find the restaurants which do not prepare any cuisine of 'American ' and achieved a grade point 'A' not belongs to the borough Brooklyn. The document must be displayed according to the cuisine in descending order.
18. Write a MongoDB query to find the restaurant Id, name, address and geographical location for those restaurants where 2nd element of coord array contains a value which is more than 42 and upto 52
db.restaurants.find({"address.coord.1":{$gt:42,$lt:52}}, {"name":1, "restaurant_id":1, "address":1}).pretty()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment