0% found this document useful (0 votes)
2 views

mongoDBhw

The document provides instructions for importing a restaurants database into MongoDB and performing various queries on it. It includes examples of how to filter and sort restaurant data based on specific criteria such as borough, cuisine, and grades. Additionally, it outlines steps for creating a new database and collection, as well as importing JSON data into MongoDB.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

mongoDBhw

The document provides instructions for importing a restaurants database into MongoDB and performing various queries on it. It includes examples of how to filter and sort restaurant data based on specific criteria such as borough, cuisine, and grades. Additionally, it outlines steps for creating a new database and collection, as well as importing JSON data into MongoDB.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

MongoDB HW

Importing the restaurants database into MongoDB

mongoimport --db restaurants --collection restaurants --file restaurants.json

⦁ Performing MongoDB Queries


Show the first 5 restaurants in the borough Bronx:

db.restaurants.find({ "borough": "Bronx" }).limit(5)

how restaurants not serving Chinese cuisine and received a grade point 'B', sorted by
cuisine in descending order:

db.restaurants.find({ "cuisine": { "$ne": "Chinese" }, "grades.grade":


"B" }).sort({ "cuisine": -1 }).limit(5)

1
Show restaurant_id, name, cuisine, and borough for the first 5 restaurants without _id
field:

db.restaurants.find({}, { "restaurant_id": 1, "name": 1, "cuisine": 1, "borough": 1, "_id": 0 }).limit(5)

Show the next 5 restaurants in the Bronx after skipping the first 5:

db.restaurants.find({ "borough": "Bronx" }).skip(5).limit(5)

2
Show restaurants in Manhattan serving American or Chinese dishes:

db.restaurants.find({ "borough": "Manhattan", "cuisine": { "$in": ["American",


"Chinese"] } }).limit(5)

Show restaurants with a score between 90 and 100:

db.restaurants.find({ "grades.score": { "$gt": 90, "$lt": 100 } }).limit(5)

3
Show restaurant id, name, borough, and cuisine for restaurants in Staten Island, Queens,
Bronx, or Brooklyn:

db.restaurants.find({ "borough": { "$in": ["Staten Island", "Queens", "Bronx", "Brooklyn"] } },


{ "restaurant_id": 1, "name": 1, "borough": 1, "cuisine": 1 }).limit(5)

Show restaurant id, name, borough, and cuisine for restaurants not in Staten Island,
Queens, Bronx, or Brooklyn:

db.restaurants.find({ "borough": { "$nin": ["Staten Island", "Queens", "Bronx", "Brooklyn"] } },

4
{ "restaurant_id": 1, "name": 1, "borough": 1, "cuisine": 1 }).limit(5)

Part B: Create your own database and collection in MongoDB (using MongoDB
Compass or mongosh). Use proper name for database and collection.

Show the contents of the original table in your MySQL table

5
Represent the contents in Figure (b) in JSON format

6
Using MongoDB Compass, import the JSON file into your collection or you can

7
manually insert documents using insertMany() (slide 43)

8
Show the contents of your collection (using find())

9
10
11
12
use projection:This query retrieves only the officeCode, city, and country fields from
each document in the OFFICES collection, omitting other fields.

use comparison operator:This query retrieves all documents from the OFFICES
collection where the country field is "USA."

13
14

You might also like