M102 WEEK 2


We will use the pcat.products collection from week 1. So start with that; if not already set up, import it:

mongoimport --drop -d pcat -c products products.json

You can find products.json from the Download Handouts link.

In the shell, go to the pcat database. If you type:

use pcat;

the shell should return 11.

Next, download homework2.js from the Download Handouts link. Run the shell with this script:

mongo --shell pcat homework2.js

First, make a mini-backup of the collection before we start modifying it. In the shell:

b = db.products_bak; db.products.find().forEach( function(o){ b.insert(o) } )
 // check it worked:
// should print 11

If you have any issues you can restore from “products_bak”; or, you can re-import with mongoimport. (You would perhaps need in that situation to empty the collection first or drop it; see the –drop option on mongoimport –help.)

In the shell, type:


What is the output? (The above will check that products_bak is populated.)

Answer : 3.05


  1. If you already have products collection from Week1 homework, go to step directly else continue to next step.
  2. Download/copy json file and paste in notepad, save as products.json.
  3. Open command prompt and Start MongoDB server using command mongod.
  4. Open another command prompt.
  5. Change to path containing products.json file.
  6. Use following command to create database “pcat” with collection “products” having documents from products.json file
mongoimport --drop -d pcat -c products products.json
  1. Start mongo shell and connect to pcat database using command
mongo pcat
  1. Use floowing command to check if you have 11 documents
  1. Exit the shell
  2. Download homework2.js from handout and store on path where products.json is stored
  3. Use following command to connect to mongo shell with homework2.js file
mongo --shell pcat homework2.js
  1. Take backup of products collection with command
b = db.products_bak; 
db.products.find().forEach( function(o){ b.insert(o) } )
  1. Count back up documents using command. It should return 11.
  1. Execute given command to find your answer.
  1. It will return answer as 3.05


Add a new product to the products collection of this form:

 "_id" : "ac9",
 "name" : "AC9 Phone",
 "brand" : "ACME",
 "type" : "phone",
 "price" : 333,
 "warranty_years" : 0.25,
 "available" : true

Note: in general because of the automatic line continuation in the shell, you can cut/paste in the above and shouldn’t have to type it all out. Just enclose it in the proper statement(s) to get it added.

Next, load into a shell variable the object corresponding to

_id : ObjectId("507d95d5719dbef170f15c00")

Then change term_years to 3 for that document. (And update it in the database.)
Then change over_rate for sms in limits to 0.01 from 0. Update that too.
At the shell prompt type:


What is the output?

Answer : 0.050.019031


  1. Start MongoDB server in command prompt using command “mongod”
  2. Start mongo shell and connect to pcat database using “mongo pcat” command
  3. Insert document in products collection as
 "_id" : "ac9",
 "name" : "AC9 Phone",
 "brand" : "ACME",
 "type" : "phone",
 "price" : 333,
 "warranty_years" : 0.25,
 "available" : true
  1. To load document matching with ObjectId() in shell variable, use command

Here, shell variable stores document matched with given “_id”

  1. Update term_years key as
  1. Update over_rate for sms in limits as
  1. Save changes into database using command
  1. This will modify matching document
  2. Run command to find your answer
  1. Your Answer is 0.050.019031


How many products have a voice limit? (That is, have a voice field present in the limits subdocument.)

Input your answer below, (just a number, no other characters).

While you can parse this one by eye, please try to use a query that will do the work of counting it for you.

Just to clarify, the answer should be a number, not a document. There should be no brackets, spaces, quotation marks, etc.

Answer : 3


  1. Considering, products collection from previous homework is available in pcat database.
  2. In mongo shell, execute command to check number of documents with voice field available in subdocument of limits key.
  1. Your answer is 3

Leave a Reply

Your email address will not be published. Required fields are marked *