Exercises sourced from: https://www.w3resource.com/mongodb-exercises/
mongod
mongoimport --db Hospality --collection restaurants --file restaurants.json
mongo
show dbs
use Hospality
show collections
- Write a MongoDB query to display all the documents in the collection restaurants
db.restaurants.find()
- 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 })
- 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, _id:0, name:1,borough:1, cuisine:1 })
- Write a MongoDB query to display the fields restaurant_id, name, borough and zip code, but exclude the field _id for all the documents in the collection restaurant.
db.restaurants.find({}, {restaurant_id:1, _id:0, name:1,borough:1, "address.zipcode":1 })
- Write a MongoDB query to display all the restaurant which is in the borough Bronx.
db.restaurants.find({"borough":"Bronx"})
- Write a MongoDB query to display the first 5 restaurant which is in the borough Bronx.
db.restaurants.find({"borough":"Bronx"}).limit(5)
- 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"}).limit(5).skip(5)
- Write a MongoDB query to find the restaurants who achieved a score more than 90
db.restaurants.find({"grades.score":{$gt:90}})
- 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}})
- Write a MongoDB query to find the restaurants which locate in latitude value less than -95.754168.
db.restaurants.find({"address.coord":{$lt : -95.754168}})
- 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.