0% found this document useful (0 votes)
206 views3 pages

Mongo DB

This document provides examples of MongoDB queries to retrieve and filter data from a restaurants data set. Some key points: - It shows how to insert data into a MongoDB collection and find/retrieve data from collections. - Many examples demonstrate querying and filtering documents based on field values, use of logical operators like $and, $or, comparisons like $gt, $lt, and projections to include or exclude fields. - Queries are sorted in various ways and data is paginated using skip and limit. - Regular expressions and substring matching are used in queries. - Geospatial queries filter locations based on latitude/longitude values. - Dates are queried using ISO date format

Uploaded by

tushar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
206 views3 pages

Mongo DB

This document provides examples of MongoDB queries to retrieve and filter data from a restaurants data set. Some key points: - It shows how to insert data into a MongoDB collection and find/retrieve data from collections. - Many examples demonstrate querying and filtering documents based on field values, use of logical operators like $and, $or, comparisons like $gt, $lt, and projections to include or exclude fields. - Queries are sorted in various ways and data is paginated using skip and limit. - Regular expressions and substring matching are used in queries. - Geospatial queries filter locations based on latitude/longitude values. - Dates are queried using ISO date format

Uploaded by

tushar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Difference between between RDBMS and MongoDB :-

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

How to insert data


db.studentData.insertOne({"name":"Tushar"},{"Class":"IBM"})

Q1. Display all the components of the file restaurants.json

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()

Q7.Write a MongoDB query to display 5 restaurant which is in the borough Bronx


after skipping the first 5

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

db.restaurants.find({grades: { $elemMatch: {"score":{$gt:9}}}}).pretty()

Q9. Write a MongoDB query to find the restaurants who have score more than 80 but
less than 100

db.restaurants.find({grades: { $elemMatch: {"score":


{$lt:100,$gt:80}}}}).pretty()

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

db.restaurants.find({"address.coord.1":{$gt : 42, $lt : 52}},{"restaurant_id"


:1,"name":1,"address":1,"coord":1}).pretty()

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

db.restaurants.find({ name: {$regex:"mon.*",$options:"i"}},


{"name":1,"borough":1,"address.coord":1,"cuisine":1}).pretty()

You might also like