Skip to content

Instantly share code, notes, and snippets.

@RitRa
Last active April 1, 2019 20:21
Show Gist options
  • Save RitRa/4683ec2e0b1c5150437e3bd630500022 to your computer and use it in GitHub Desktop.
Save RitRa/4683ec2e0b1c5150437e3bd630500022 to your computer and use it in GitHub Desktop.

Exercises sourced from: https://www.w3resource.com/mongodb-exercises/

Import json

mongod
mongoimport --db Hospality --collection restaurants --file restaurants.json
mongo
show dbs

use Hospality
show collections
  1. Write a MongoDB query to display all the documents in the collection restaurants
db.restaurants.find()
  1. 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 })
  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 })
  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 })
  1. Write a MongoDB query to display all the restaurant which is in the borough Bronx.
db.restaurants.find({"borough":"Bronx"})
  1. Write a MongoDB query to display the first 5 restaurant which is in the borough Bronx.
db.restaurants.find({"borough":"Bronx"}).limit(5)
  1. 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)
  1. Write a MongoDB query to find the restaurants who achieved a score more than 90
db.restaurants.find({"grades.score":{$gt:90}})
  1. 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}})
  1. 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}})
  1. 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment