$29
Overview
This project will give you hands-on practice in working with the MongoDB “NoSQL” database software.
Citation:
Much gratitude goes out to CS Masters student Ganesh Chandra Satish. Ganesh prepared this
MongoDB lab for us. Thank you Ganesh !!!
Objectives
1. Become familiar with MongoDB
2. Install MongoDB on your computer (Windows, Linux, Mac)
3. Create and load a MongoDB database
4. Perform several basic operations against your MongoDB database
5. Have fun !!
Deliverables
Capture screen shots to show evidence of having completed the assigned operations (1 – 8) described
below. Number each screen shot with the number of the assigned operation/task (1 – 8). Assemble
(Copy & Paste) all screen shots into a document. Save the document as a PDF.
Submission
Use the submission link in the Project Assignment section of the Week 14 Moodle April 16-22 -- which is
the same place where you got this file.
If you are doing “PAIR PROGRAMMING” on this assignment, please be sure to identify the name(s) of
your “programming” partner(s) on your submission.
You must EACH submit your own final deliverable document for this homework.
CSCI3287 Database Systems
Project 2 – NOSQL Database Lab -- MongoDB
CSCI 3287 Database Systems Page 2
Introduction:
The following links are helpful for giving an Introduction and basic queries for mongoDB:
1. https://www.tutorialspoint.com/mongodb/mongodb_overview.htm : This link provides a great
basic overview of mongodb and the basic queries for inserting a document, updating a
document, querying/searching a collection, etc.
2. https://www.guru99.com/mongodb-tutorials.html#1 : This is a beginner tutorial and also has
links for installing and running mongoDB with images which may be helpful for students initially.
3. https://docs.mongodb.com/manual/introduction/ : The official documentation page of
mongoDB.
Installation:
The following links are helpful for giving Instructions for the installation of mongoDB:
1. MacOS: https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x/ : One input is
that the .bashrc file in mac is /Users/apple/.bash_profile.
2. Windows: https://docs.mongodb.com/tutorials/install-mongodb-on-windows/ : This is the
official documentation for windows installation.
3. Linux: https://docs.mongodb.com/manual/administration/install-on-linux/ : The official
installation for linux installation.
4. https://docs.mongodb.com/manual/installation/ : This link basically shows the details about the
operating systems supported by mongoDB.
5. Students can install the community edition of mongoDB as it is freely available.
CSCI3287 Database Systems
Project 2 – NOSQL Database Lab -- MongoDB
CSCI 3287 Database Systems Page 3
Database Operations:
1. Create a database:
a. use DATABASE_NAME
i. Ex: use new_mongo_db
ii. The above command will create a database if it does not exist and uses the
database if it already exists.
iii. Replace the DATABASE_NAME with the name of the database you would want to
create.
2. Drop a database:
a. db.dropDatabase()
i. Ex: use new_mongo_db
switched to db new_mongo_db
db.dropDatabase()
ii. First you need to switch to the database that has to be dropped. The use the
above command to drop that database.
3. Creating a collection:
a. db.createCollection(name, options)
i. Ex: use new_mongo_db
switched to db new_mongo_db
db.createCollection("test_collection")
{ "ok" : 1 }
ii. The above command creates the collection. But giving some initial options along
with the “create” will be highly useful.
1. db.createCollection("mycol", { capped : true,
autoIndexId : true, size :
6142800, max : 10000 } )
{ "ok" : 1 }
2. For more information on the options, please check the following link.
a. https://docs.mongodb.com/manual/reference/method/db.crea
teCollection/
iii. In mongoDB, it is not necessary to create a collection. When a new document is
inserted, mongoDB creates a collection automatically.
iv.
4. Dropping a collection:
CSCI3287 Database Systems
Project 2 – NOSQL Database Lab -- MongoDB
CSCI 3287 Database Systems Page 4
a. db.COLLECTION_NAME.drop()
i. Ex: use new_mongo_db
switched to db new_mongo_db
db.test_collection.drop()
True
ii. First go to the selected database and then use the above command to drop the
collection
iii.
5. Insert a document:
a. db.COLLECTION_NAME.insert(document)
i. Ex: db.test_collection.insert({
_id: ObjectId(7df78ad8902c),
title: ‘Mongo Db practice’,
description: ‘this class is about MongoDB’
})
ii. Replace the COLLECTION_NAME with the name of the collection of your choice
iii.
6. Query a document:
a. db.COLLECTION_NAME.find()
i. Ex: db.test_collection.find().pretty()
ii. The above query will display the documents present in the collection.
iii.
7. Update a document:
a. db.test_collection.update(SELECTION_CRITERIA, UPDATED_DATA)
i. Ex: db.test_collection.update({'Heading':'MongoDB
Tutorials'},{$set:{'Heading':'New MongoDB Tutorials'}})
ii. The above example is used to update the documents that contain ‘Heading’ as
'MongoDB Tutorials' to 'New MongoDB Tutorials'
8. Delete Document:
a. db.COLLECTION_NAME.remove(DELLETION_CRITTERIA)
i. Remove only one record:
1. db.test_collection.remove({ status : "P" },1)
2. Here the first document which has this key value pair will be deleted.
ii. Remove all records matching a condition:
1. db.test_collection.remove({ status : "P" })
2. Here all the documents which have this key value pair will be deleted.
CSCI3287 Database Systems
Project 2 – NOSQL Database Lab -- MongoDB
CSCI 3287 Database Systems Page 5
Other References:
a. https://www.tutorialspoint.com/mongodb/mongodb_create_database.htm
b. https://docs.mongodb.com/v3.2/crud/
Appendix A – sample Data
Dataset for the students to play with in MongoDB:
1. Download the json dataset from(save the file shown)
https://raw.githubusercontent.com/mongodb/docs-assets/primer-dataset/primer-dataset.json
2. From your terminal enter the command:
a. mongoimport --db test --collection restaurants --drop --
file ~/downloads/primerdataset.json
b. The above command converts the json file and stores it as a set of documents with the
collection name of “restaurants”.
c. Then in the mongoDB terminal you can query the collections.
i. Ex: db.restaurants.find().pretty()
ii. The above command will display 20 records of the entire dataset.
3. Each document looks like the one below: