M102 WEEK 3

HOMEWORK 3.1

Start a mongod server instance (if you still have a replica set, that would work too).

Next, download the handout and run:

mongo --shell localhost/performance performance.js
homework.init()

Build an index on the “active” and “tstamp” fields. You can verify that you’ve done your job with

db.sensor_readings.getIndexes()

When you are done, run:

homework.a()

and enter the numeric result below (no spaces).

Note: if you would like to try different indexes, you can use

db.sensor_readings.dropIndexes() to drop your old index before creating a new one. (For this problem you will only need one index beyond the _id index which is present by default.)

ANSWER: 6

Steps:

  1. Open command prompt and start MongoDB server
  2. Download handout and save as performance.js
  3. Go to path where performance.js is stored and execute following commands one by one
> mongo --shell localhost/performance performance.js
> homework.init()
  1. At the end, you will get value as count : 20000
  2. Then create index on “active” and “tstamp” keys as below
> db.sensor_readings.createIndex( { "active" : 1 , "tstamp" : 1 } )
  1. Above command will create an index on both keys. Execute following command to check if indexes are created.
> db.sensor_readings.getIndexes()
  1. After checking available indexes on “sensor_readings” collection, execute following command to check your answer.
> homework.a()
  1. Your answer is 6.

HOMEWORK 3.2

In a mongo shell run homework.b(). This will run in an infinite loop printing some output as it runs various statements against the server.

We’ll now imagine that on this system a user has complained of slowness and we suspect there is a slow operation running. Find the slow operation and terminate it.

In order to do this, you’ll want to open a second window (or tab) and there, run a second instance of the mongo shell, with something like:

$ mongo --shell localhost/performance performance.js

Keep the other shell with homework.b() going while this is happening. Once you have eliminated the slow operation, run (on your second tab):

homework.c()

and enter the output below. Once you have it right and are ready to move on, ctrl-c (terminate) the shell that is still running the homework.b() function.

ANSWER: 12

HOMEWORK 3.3

Download and extract the json file in products.zip

Then perform the following in the terminal (or at the command prompt):

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

If that looks somewhat familiar, that’s because it’s (nearly) the same command you used to import the pcat.products collection for Homework 2.1, with the only difference in the command being that it will drop the collection if it’s already present. This version of the collection, however, contains the state of the collection as it would exist once you’ve solved all of the homework of chapter 2.

Next, go into the pcat database.

mongo pcat

Create an index on the products collection for the field, “for”.

After creating the index, do a find() for products that work with an “ac3” phone (“ac3” is present in the “for” field).

Q1: How many products match this query?

Q2: Run the same query, but this time do an explain(). How many documents were examined?

Q3: Does the explain() output indicate that an index was used?

  • Q1:0
  • Q1:1
  • Q1:3
  • Q1:4
  • Q2:1
  • Q2:4
  • Q2:5
  • Q2:12
  • Q3:NO
  • Q3:YES

ANSWER:

  • Q1:4
  • Q2:4
  • Q3:YES

Steps:

  1. Start MongoDB server in a command prompt
  2. Download handout and extract “products.json” file from the download
  3. Open another command prompt and move to path where “products.json” is stored.
  4. Execute following command to import documents from json file in to database and then connect to “pcat” database
> mongoimport --drop -d pcat -c products products.json
> mongo pcat
  1.  Create index on “for” key of “products” collection as
> db.products.createIndex( { "for" : "ac3" } )
  1. To find answer of first question, execute following command which gives number of documents matching query where “for” is equal to “ac3”.
> db.products.find({"for":"ac3"}).count()
  1. This will give answer as “4”.
  2. To find answer of second question, it is necessary to find number of documents scanned by above query.
  3. This can be performed using following query
> db.products.find({"for":"ac3"}).explain("executionStats")
  1. In output, check number of keysExamined value which returns answer as “4”.
  2. For question three, check value for indexName key which gives name of index used. And so your answer is “yes” for this question.

HOMEWORK 3.4

Which of the following are available in WiredTiger but not in MMAPv1? Check all that apply.

  • DOCUMENT LEVEL LOCKING
  • DATA COMPRESSION
  • INDEXES
  • COLLECTION LEVEL LOCKING
  • COVERED QUERIES

ANSWER :

  • DOCUMENT LEVEL LOCKING
  • DATA COMPRESSION

Leave a Reply

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