Mongo DB
Mongo DB
RDBMS
Database
Table view
Row
Column
Index
Join
Foreign Key
Partition Key
Mongo DB
Database
Collection
Documents(JSON,BSON)
Field
Index
Embedded Document
Reference
Shard Key
db.restaurants.find()
> show dbs
use mongo(DB name)
db.restaurants(collection name)
db.restaurants.find()
db.restaurants.find().pretty()
Q2. Write a mongoDb query to display fields restaurants_id borough and cusine for
all documents in the collection restaurants
db.restaurants.find({},{"restaurant_id" : 1,"name":1,"borough":1,"cuisine" :
1})
Q3. Write a mongoDb query to display fields restaurants_id borough and cusine for
all documents in the collection restaurants and exclude the field id
db.restaurants.find({},{"restaurant_id" : 1,"name":1,"borough":1,"cuisine" :
1," _id":0}).pretty()
Q4. Write a mongoDb query to display fields restaurants_id borough and cusine for
all documents in the collection restaurants and exclude the field id and adding
zipcode instead of cuisine
db.restaurants.find({},{"restaurant_id" :
1,"name":1,"borough":1,"address.zipcode" :1,"_id":0}).pretty()
Q5. Write a mongo db query to display the entire restaurant which is in the borough
Bronx
db.restaurants.find({"borough": "Bronx"}).pretty()
Q6. Write a MongoDB query to display the first 5 restaurant which is in the borough
Bronx
db.restaurants.find({"borough": "Bronx"}).limit(5).pretty()
db.restaurants.find({"borough": "Bronx"}).skip(5).limit(5).pretty()
Q8. Write a MongoDB query to find the restaurant who achieved score of more than 9
Q9. Write a MongoDB query to find the restaurants who have score more than 80 but
less than 100
Q10. 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": {$lt:-65.754168}}]}).pretty()
Q11. Write a MongoDB query to find restaurants which do not prepare any cuisine of
american and achieve score more than 70 and not located in the latitude less than
-65.754168
db.restaurants.find({$and: [{"cuisine":{$ne:"American"}},{"grades.score":
{$gt:70}}, {"address.coord": {$gt:-65.754168}}]}).pretty()
Q12. Write a MongoDB query to find the restaurants which do not prepare any cuisine
of American and achieve grade point A not belong to the borough Brooklyn . The
document must be displayed according to the cuisine in decending order
db.restaurants.find({$and: [{"cuisine":{$ne:"American"}},
{"grades.grade":"A"},{"borough":{$ne:"Brooklyn"}}]}).pretty()
db.restaurants.find({$and: [{"cuisine":{$ne:"American"}},
{"grades.grade":"A"},{"borough":{$ne:"Brooklyn"}}]}).sort({"cuisine":-1}).pretty()
Q13. Write a MongoDB query to find rastaurants id,name,borough and cuisine for
those restaurants which contain "Wil" as first three letters in its name
db.restaurants.find({name:/^Wil/},
{"restaurant_id":1,"name":1,"borough":1,"cuisine":1}).pretty()
Q14. Write a MongoDB query to find rastaurants id,name,borough and cuisine for
those restaurants which contain "ces" as last three letters in its name
Q15. Write a MongoDB query to find the restaurant id , name , and grades for those
restaurants which achieved a grade of "A" and scored 11 on an ISO date "2014-08-11
T00:00:00Z" among many of survey dates
db.restaurants.find({$and : [{"grades.date":ISODate("2014-08-11T00:00:00Z")},
{"grades.grade":"A"},{"grades.score":11}]}).pretty()
Q16. Write a MongoDb query to find a 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
Q17. Write a MongoDB query to arrange the name of the restaurants in ascending
order along with all the columns
db.restaurants.find().sort({"name":1}).pretty()
For descending db.restaurants.find().sort({"name":-1}).pretty()
Q18.Write a MongoDB query to arrange the name of the cuisine in ascending order and
for that same cuisine borough should be in decending order
db.restaurants.find().sort({"cuisine":1,"borough":-1}).pretty()
Q19. Write a MongoDB query to know whether all the address contains street or not
db.restaurants.find({"address.street":{$exists : true}}).pretty()
Q20. Write a MongoDB query which will select the restaurant id, name ,grades for
those restaurants which returns 0 as a reminder after dividing the score by 7
db.restaurants.find({"grades.score":{$mod:[7,0]}},
{"restaurant_id":1,"name":1,"grades":1}).pretty()
Q21. Write a MongoDb query to find the restaurant name ,borough , longitude and
altitude and cuisine for those restaurants which contains 'mon' as three letters
somewhere in its name