Thursday, 7 May 2015

MongoDB M101N Final Exam Question 10

Question 10:

We perform the following query on the enron dataset:

var exp = db.messages.explain('executionStats')

exp.find( { 'headers.Date' : { '$gt' : new Date(2001,3,1) } }, { 'headers.From' : 1, '_id' : 0 } ).sort( { 'headers.From' : 1 } )


and get the following explain output.  

MongoDB M101N Final Exam Question 9

Question 9:

Imagine an electronic medical record database designed to hold the medical records of every individual in the United States. Because each person has more than 16MB of medical history and records, it's not feasible to have a single document for every patient. Instead, there is a patient collection that contains basic information on each person and maps the person to a patient_id, and a record collection that contains one document for each test or procedure. One patient may have dozens or even hundreds of documents in the record collection.
We need to decide on a shard key to shard the record collection. What's the best shard key for the record collection, provided that we are willing to run inefficient scatter-gather operations to do infrequent research and run studies on various diseases and cohorts? That is, think mostly about the operational aspects of such a system. And by operational, we mean, think about what the most common operations that this systems needs to perform day in and day out.

MongoDB M101N Final Exam Question 8

Question 8:

Supposed we executed the following C# code. How many animals will be inserted into the "animals" collection?

using MongoDB.Bson;
using MongoDB.Driver;
using System.Threading.Tasks;

namespace M101DotNet
{
    class InsertTest
    {
        static void Main(string[] args)
        {
            MainAsync(args).GetAwaiter().GetResult();
        }

        static async Task MainAsync(string[] args)
        {
            var client = new MongoClient();
            var db = client.GetDatabase("test");

            var animals = db.GetCollection<BsonDocument>("animals");

            var animal = new BsonDocument
                            {
                            {"animal", "monkey"}
                            };

            await animals.InsertOneAsync(animal);
            animal.Remove("animal");
            animal.Add("animal", "cat");
            animal["_id"] = ObjectId.GenerateNewId();
            await animals.InsertOneAsync(animal);
            animal.Remove("animal");
            animal.Add("animal", "lion");
            animal["_id"] = ObjectId.GenerateNewId();
            await animals.InsertOneAsync(animal);
        }
    }
}

MongoDB M101N Final Exam Question 7

Question 7:

You have been tasked to cleanup a photo-sharing database. The database consists of two collections, albums, and images. Every image is supposed to be in an album, but there are orphan images that appear in no album. Here are some example documents (not from the collections you will be downloading). 

> db.albums.findOne()
{
    "_id" : 67
    "images" : [
        4745,
        7651,
        15247,
        17517,
        17853,
        20529,
        22640,
        27299,
        27997,
        32930,
        35591,
        48969,
        52901,
        57320,
        96342,
        99705
    ]
}

> db.images.findOne()
{ "_id" : 99705, "height" : 480, "width" : 640, "tags" : [ "dogs", "kittens", "work" ] }


Wednesday, 6 May 2015

MongoDB M101N Final Exam Question 6

Question 6:

Suppose you have a collection of students of the following form:
{
 "_id" : ObjectId("50c598f582094fb5f92efb96"),
 "first_name" : "John",
 "last_name" : "Doe",
 "date_of_admission" : ISODate("2010-02-21T05:00:00Z"),
 "residence_hall" : "Fairweather",
 "has_car" : true,
 "student_id" : "2348023902",
 "current_classes" : [
  "His343",
  "Math234",
  "Phy123",
  "Art232"
 ]
}

Now suppose that basic inserts into the collection, which only include the last name, first name and student_id, are too slow (we can't do enough of them per second from our program). What could potentially improve the speed of inserts. Check all that apply.

Tuesday, 5 May 2015

MongoDB M101N Final Exam Question 5

Question 5:

Suppose your have a collection fubar with the following indexes created:

[
    {
        "v" : 1,
        "key" : {
            "_id" : 1
        },
        "ns" : "test.fubar",
        "name" : "_id_"
    },
    {
        "v" : 1,
        "key" : {
            "a" : 1,
            "b" : 1
        },
        "ns" : "test.fubar",
        "name" : "a_1_b_1"
    },
    {
        "v" : 1,
        "key" : {
            "a" : 1,
            "c" : 1
        },
        "ns" : "test.fubar",
        "name" : "a_1_c_1"
    },
    {
        "v" : 1,
        "key" : {
            "c" : 1
        },
        "ns" : "test.fubar",
        "name" : "c_1"
    },
    {
        "v" : 1,
        "key" : {
            "a" : 1,
            "b" : 1,
            "c" : -1
        },
        "ns" : "test.fubar",
        "name" : "a_1_b_1_c_-1"
    }
]


Monday, 4 May 2015

MongoDB M101N Final Exam Question 4

Question 4:

For this problem, you will be implementing the functionality needed to "like" a comment in the blog.
First, download and unpack the blog solution handout from here
Next, find the "XXX" in HomeController.cs. This is where you will need to code in the ability to "like" a comment.
When you are done, the following functionality will be in place:

  • When you are logged in, and you view a post in its own page (/Home/Post/<post_id>), each post will have a number of "Likes".
  • When you click on the "Likes" button, it gets incremented by one.
  • You can like a comment as many times as you wish; each click will increment it by one.
  • You can even like your own comments.
In your "blog.posts" documents, the "Comments" subdocuments will each contain a "Likes" field. Its value will be a number, and that number will be the number of "Likes" for that comment. Here is an example: