M102 WEEK 1

HOMEWORK 1.1

Download and install MongoDB from www.mongodb.org. Then run the database as a single server instance on your PC (that is, run the mongod binary). Then, run the administrative shell.

From the shell prompt type

db.isMaster().maxBsonObjectSize

at the “>” prompt.

What do you get as a result?

Answer is : 16777216

Steps:

  1. Start MongoDB server using “mongod” command on command prompt.
  2. Start mongo shell using “mongo” command on another command prompt.
  3. On shell prompt, type following command :
> db.isMaster.maxBsonObjectSize()
  1. Your answer is : 16777216

HOMEWORK 1.2

Download the handout. Take a look at its content.

Now, import its contents into MongoDB, into a database called “pcat” and a collection called “products”. Use the mongoimport utility to do this.

When done, run this query in the mongo shell:

db.products.find( { type : "case" } ).count()

What’s the result?

Answer is : 3

Steps:

  1. Copy contents of handout in notepad and save as products.json.
  2. Go to path where json file is stored and import contents of json file in database “pcat” and in collection “products”.
mongoimport --drop -d  pcat  -c products products.json
  1. This command will import 11 documents in your collection.
  2. Start your MongoDB server using command “mongod”
  3. Start mongo shell using command “mongo”.
  4. Switch to database pcat as
> use pcat
  1. Execute command
db.products.find({type: "case" }).count()
  1. Your answer is 3.

Homework 1.3

At this point you should have pcat.products loaded from the previous step. You can confirm this by running in the shell:

db.products.find()
// or:
db.products.count()
// should print out “11”

Now, what query would you run to get all the products where brand equals the string “ACME”?

Answer is  :

 db.products.find({brand: "ACME" })

Steps:

  1. Start your MongoDB server using command “mongod”
  2. Start mongo shell using command “mongo”.
  3. Switch to database pcat as
> use pcat
  1. Execute command
db.products.find({brand: "ACME" })
  1. You will get all documents matching with brand name as “ACME”.

Homework 1.4

How would you print out, in the shell, just the value in the “name” field, for all the product documents in the collection, without extraneous characters or braces, sorted alphabetically, ascending? (Check all that would apply.)

  • products.find( { }, { name : 1, _id : 0 } ).sort( { name : 1 } )
  • var c = db.products.find( { }, { name : 1, _id : 0 } ).sort( { name : 1 } );

while( c.hasNext() ) {
print( c.next().name);
}

  • var c = db.products.find( { } ).sort( { name : 1 } );

c.forEach( function( doc ) { print( doc.name ) } );

  • var c = db.products.find( { } ).sort( { name : -1 } );

while( c.hasNext() ) {
print( c.next().name);
}

Answer is :

  • var c = db.products.find( { }, { name : 1, _id : 0 } ).sort( { name : 1 } );

while( c.hasNext() ) {
print( c.next().name);
}

  • var c = db.products.find( { } ).sort( { name : 1 } );

c.forEach( function( doc ) { print( doc.name ) } );

Leave a Reply

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