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

hw4 Mongodb

The document provides instructions for an assignment using MongoDB. It includes objectives to learn MongoDB CRUD methods and MapReduce. It outlines how to set up MongoDB locally and load sample data. It describes 3 parts to the assignment: 1) practicing CRUD operations, 2) writing a MapReduce script to count customers by zip code, and 3) using MapReduce to join and summarize order quantities by zip code. An extra credit part involves calculating average order quantity. Students are to submit the 4 MapReduce scripts.

Uploaded by

api-526395450
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
149 views

hw4 Mongodb

The document provides instructions for an assignment using MongoDB. It includes objectives to learn MongoDB CRUD methods and MapReduce. It outlines how to set up MongoDB locally and load sample data. It describes 3 parts to the assignment: 1) practicing CRUD operations, 2) writing a MapReduce script to count customers by zip code, and 3) using MapReduce to join and summarize order quantities by zip code. An extra credit part involves calculating average order quantity. Students are to submit the 4 MapReduce scripts.

Uploaded by

api-526395450
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

CST363 Mongo DB

Objective

• Learn to use a popular document database MongoDB


• Learn to use the CRUD (create, retrieve, update, delete) methods of Mongo DB
• Code a Map Reduce program for Mongo DB
• Use Map Reduce and sort-join join method to join two collections.

Setup

• download mongodb https://www.mongodb.com/download-center/community


• use the zip or targz file format (not windows msi format)
• the mongo/bin directory contains mongo programs
• to start the server: > mongod
• to start the shell: > mongo
• by default the shell will create a use a mongodb database named “test”
• the shell command use foo will switch to or create a database “foo”
• to run a script file > mongo script.js

Requirements and Directions

• download the files from iLearn with sample data: customers_load.js and orders_load.js
• You can use the “test” database or create a new database. Run the scripts to create and load
collections “customers” and “orders”

mongo customers_load.js
mongo orders_load.js

• Familarize yourself with the customer and order document schema (see page 3)

1
CST363 Mongo DB

Part 1

• familiarize youself with mongodb shell commands to insert, retrieve, update and delete
documents.

1. using the insert method, create 3 documents in a collection named “patients”. Each patient
document has attributes: name, ssn, age, address. Patients ages should be 10, 20 and 30.
2. The patient with age 30 has a list of prescriptions

prescriptions : [
{ id: "RX743009", tradename : "Hydrochlorothiazide" },
{ id : "RX656003", tradename : "LEVAQUIN", formula : "levofloxacin" }
]

3. Retrieve and list all patient data.


4. Retrieve the patient document whose age is equal to 20.
5. Retrieve the patients where age is less than 25.
6. Using the drop method to delete the entire collection.
7. Convert the commands you used in steps 1-9 into a script1.js file. Read the next section “Coding
a mongo script file”. Test your script using the command
mongo --db=test script1.js

note: if the script1 file is not in the same directory as the mongo executable, you will have to give
directory path.

Coding a mongo script.js file

• When using the interactive shell, the shell will automatically print out results of commands and
queries. But when used in a script there will be no output.
• To print an object use printjson( ) statement
doca = db.col.findOne({name:”tom”})
print(“Document with name tom”)
printjson(doca)

• To print out a value such as a string or number


print(“hello world”)

• The find( ) command returns a cursor over a list of objects. To print the objects use the code
cursor = db.col.find({name:”tom”})
print(“Displaying all documents with name tom”)
while (cursor.hasNext()){
printjson(cursor.next())
}

2
CST363 Mongo DB

Part 2

• code a script2.js file that does a map reduce of the customers collections and produces a report
that shows zip code that start with ‘9’ and the count of customers for each zip code.

Part 3

• code a script3.js file that does uses map reduce to do a join of the customers and orders
collectons and summarizes the quantity of all items sold by zip code. Your output should have
for each zip code, the count of items sold to customers in that zip code.

Extra Credit (+20 points)

• code a script4.js file that does a map reduce that answers this question? What is the average
quantity for orders? If an order includes

item 1, quantity 4

item 2, quantity 1

there is a total quantity of 5 (4+1) in this order.

Your script calculates the average quantity of all order should print out a single number which is
the average of all orders.

What to submit for this assignment?

The 3 script files for map reduce script1.js, script2.js, script3.js If you do the extra credit, then you
will also submit a file script4.js

3
CST363 Mongo DB

Mongo Reference of common comands

> mongod start the server

> mongo start the shell

> quit() quit the shell

> db displays the current database being used

> use foobar switches database to foobar. Creates foobar if it does not exist.
> db.coll.insert( object )

inserts a document into collection “coll”. If “coll” does not exist, it is created.

> db.coll.find( ) displays all documents in collection “coll”

> db.coll.findOne( ) displays one documents in collection “coll”

> db.coll.update( {attribute: value}, object ) update the document

> db.coll.remove(object ) delete the document

> db.coll.drop( ) drops collection “coll”


> db.coll.mapReduce( mapF, reduceF, {out: {reduce: “mr_coll”}})

performs map reduce operation over collection “coll” using map function “mapF” and
reduce function “reduceF”, the output is merged into collection “mr_coll” using the
reduce function to combine entries.
> db.coll.mapReduce( mapF, reduceF, {out: “mr_coll”})

performs map reduce operation over collection “coll” using map function “mapF” and
reduce function “reduceF”. The ouptut if written to collection “mr_coll”. If this
collection already exists, it is replaced.

4
CST363 Mongo DB

A sample customer document


> db.customers.findOne()
{
"_id" : ObjectId("5e8233da9c97cf2cbbf58c30"),
"customerId" : 6,
"customer_name" : "California Chamber Of Commerce",
"address" : {
"street" : "3255 Ramos Cir",
"city" : "CA",
"state" : "Sacramento",
"zip" : "95827"
},
"contact" : {
"last_name" : "Mauro",
"first_name" : "Anton"
}
}

A sample order document


Most orders have only a single item. But some have multiple items.
{
"_id" : ObjectId("5e823dc1cc7841f049f2ab76"),
"orderId" : 12,
"customer" : 96,
"date" : "2018-04-26",
"total" : 662,
"items" : [
{
"itemNo" : 5,
"qty" : 1
},
{
"itemNo" : 6,
"qty" : 2
},
{
"itemNo" : 7,
"qty" : 3
},
{
"itemNo" : 8,
"qty" : 1
}
]
}

You might also like