$29.99
DBS311 – Advanced Data Systems
1 | P a g e
Lab 8 – MongoDB – Query
Objective
In this Lab, you learn to query a database in MongoDB.
Submission
For this lab, you should submit a file with the below exercises completed.
Your file should be called: L08–lastname-firstname (for example: L08-King-Les)
Make sure to show your output for each command to demonstrate it works.
Getting Started
In this lab, you will use products.json dataset. Download products.json from Blackboard and
store it in a folder named dataset.
Open your Windows command prompt and go the following directory where MongoDB is
installed:
➢ cd C:\Program Files\MongoDB\Server\4.2\bin
To run MongoDB, execute mongod
➢ mongod
When MongoDB starts successfully, open another Windows command prompt and go the same
bin directory:
➢ cd C:\Program Files\MongoDB\Server\4.2\bin
and execute mongo
➢ mongo
Or you execute a batch file to start up MongoDB.
DBS311 – Advanced Data Systems Summer 2020
2 | P a g e
You will import products.json to the inventory database. To import data, go to the bin directory:
➢ cd C:\Program Files\MongoDB\Server\4.2\bin
Execute the following command:
➢ mongoimport --db inventory --collection products --file ..\dataset\products.json
For the json file, provide the full path to the products.json. After executing the command, the
data is imported to the inventory database. To make sure data is imported successfully, go to the
MongoDB shell and execute the following command to see the imported documents:
➢ show dbs
You should see the database inventory added to the list of your databases. To see the documents
inside the database:
➢ use inventory
➢ db.products.find().forEach(printjson)
Tasks
1. Write a query to return name and price of each product in the inventory database.
2. Write a query to return name and price for products of type accessory in the inventory
database.
3. Write a query to return name and price for products with price between $12 and $20
(Values 12 and 20 are included).
DBS311 – Advanced Data Systems Summer 2020
3 | P a g e
4. Write a query to return id, name, price, and type for products that are not of type
accessory.
5. Write a query to return id, name, price, and type for products with type accessory or
service.
6. Write a query to return id, name, price, and type for products that do have the type key.
7. Write a query to return id, name, price, and type for products that their type is both
accessory and case.