Wednesday, 13 April 2016

Quiz and Homework of MongoDB

Quiz: Concepts

Q.>What were the big differences in hardware over the last few decades that MongoDB attempted to address?


1.Parallelism of cores
2.Parallelism of servers
3.Quantum computers
4.Faster clock speeds
5.More memory

Ans.

1.Parallelism of cores
2.Parallelism of servers


Quiz: Scaling


Q: When scaling out horizontally (adding more servers to contain your data), what are problems that arise as you go from, say, 1 commodity server to a few dozen?

1. The original server, if incorporated into the cluster, is more likely to fail in a given unit of time than if it had been left as a single server.
2. The servers must communicate with one another eating up network bandwidth
3. The need for redundancy increases as the likelihood of some failure in the system per unit of time increases.
4. Hardware cost per server is likely to increase.

Ans
2. The servers must communicate with one another eating up network bandwidth
3. The need for redundancy increases as the likelihood of some failure in the system per unit of time increases.


Quiz: SQL and Complex Transactions

Q: What causes significant problems for SQL when you attempt to scale horizontally (to multiple servers)?

1. Joins
2. Queries
3. Transactions
4. Inserts
5. Indexes


Ans.

1. Joins
3. Transactions


Quiz: Documents Overview

Q.> What are some advantages of representing our data using a JSON-like format?


1. JSON presents a flexible and concise framework for specifying queries as well as storing records.
2. JSON syntax is similar to that of common data structures used in many programming languages and is, therefore, familiar to developers.
3. JSON is language independent.
4. JSON is optimized for use with JavaScript.

Ans.

1. JSON presents a flexible and concise framework for specifying queries as well as storing records.
2. JSON syntax is similar to that of common data structures used in many programming languages and is, therefore, familiar to developers.
3. JSON is language independent.


Quiz: JSON Syntax
Quiz: JSON Types

Q.> How many data types are there in JSON?

a. 1 (just strings)
b. 2
c. 4
d. 6


Ans

d. 6



Q.> What is the corresponding JSON for the following XML document?

<person>
  <name>John</name>
  <age>25</age>
  <address>
    <city>New York</city>
    <postalCode>10021</postalCode>
  </address>
  <phones>
    <phone type="home">212-555-1234</phone>
    <phone type="mobile">646-555-1234</phone>
  </phones>
</person>


Ans

{
"name" : "John",
"age" : 25,
"address" : { "city" : "New York", "postalCode" : "10021" },
"phones" : [ {"phone":"212-555-1234", "type" : "home"}, {"phone":"646-555-1234", "type" : "mobile"} ]
 }



Quiz: JSON Syntax 2


Q.> For the following XML, Is the corresponding JSON example legal json?
<things>
  <hat>one</hat>
  <coat>z</coat>
  <hat>two</hat>
</things>
{
  "hat" : "one",
  "coat" : "z",
  "hat" : "two"
}

1. Yes
2. No
3. Maybe

Ans

3. Maybe



Quiz: Binary JSON


Q.> Why do we represent our data as BSON rather than JSON in the system?


  1. Fast machine scanability.
  2. Human readability.
  3. Stronger typing (and more types) than JSON

 Ans.

1. Fast machine scanability.
3. Stronger typing (and more types) than JSON




Quiz: BSON and applications


Q.> For a typical client (a python client, for example) that is receiving the results of a query in BSON, would we convert from BSON to JSON to the client's native data structures (for example, nested dictionaries and lists in Python), or would we convert from BSON straight to those native data structures?
  
  1.  BSON -> Native data structures
  2. BSON -> JSON -> Native data structures


 Ans.

  1. BSON -> Native data structures




Quiz: Dynamic Schema


Q.> True or False: MongoDB is schemaless because a schema isn't very important in MongoDB


  1. True
  2. False

 Ans. 

2. False



Quiz: What is the MongoDB shell?


Q: By default, which database does the mongo shell connect to?


  1. test
  2. localhost
  3. mongo
  4. help
  5. default

 Ans

  1. test




Quiz: Mongoimport


The mongoimport utility can import what types of data?


  1. JSON
  2. CSV
  3. BSON
  4. TSV
  5. XML

 Ans.

  1. JSON
  2. CSV
  3. BSON
  4. TSV



Quiz: Cursors Introduction


In order to query a collection in the mongo shell, we can type which of the following?


  1. db.collection.find()
  2. db.find()
  3. db.collection.get()
  4. db.collection.select(*)
  5. db.collection.get(true)



 Ans.

5. db.collection.find()



None: Query Language: Basic Concepts

Q.> You have a collection where every document has the same fields, and you want to look at the value of the “_id”, "name", and “email” fields in order to see their format. Furthermore, you want to eliminate all other fields from the query results. What query might you write?


  1. db.collection.find({ name : 1, email: 1 }, {} )
  2. db.collection.find( { } , { name: 1 , email: 1 } )
  3. db.collection.find( { name: 1 } , { email : 1 } )
  4. db.collection.find( {name: 1 , email: 1 } )

 Ans.

2. db.collection.find( { } , { name: 1 , email: 1 } )



Quiz: Query Language: Projection


Q.> You want to query the “people” collection, you want the results of the query to include only documents where age is 50, and you want to look at all fields except “email”. What query should you write?


  1. db.people.find( { email : 0, age : 50 } , { } )
  2. db.people.find( { email : 0 }, { age : 50 } )
  3. db.people.find( { age : 50 } , {email: 0 } )
  4. db.people.find( { } , { email : 0 , age : 50 } )
  5. db.people.find( { age : 50 }, {email : 1} )

 Ans.

3. db.people.find( { age : 50 } , {email: 0 } )



Quiz: Query Language: Advantages of a Dynamic Schema


Q.> If you want to add a new key: value pair to the documents in the “shapes” collection, what methods could you use?


  1. db.shapes.alterCollection()
  2. db.shapes.update()
  3. db.shapes.resizeObject()
  4. db.shapes.find()
  5. db.shapes.save()

 Ans.

2. db.shapes.update()
4. db.shapes.save()


Quiz: Sorting


Q.> If you want to run a query on the collection, “books,” and sort ASCIIbetically by title on the query, which of the following will work?


  1. db.books.find( { } , { sort : { title : 1 } } )
  2. db.books.find( { } , { title : "$sort" } )
  3. db.books.find( { } ).sort( { title : -1 } )
  4. db.books.sort( { title : 1 } ).find( { } )
  5. db.books.find().sort( { title : 1 } )
Ans.


5. db.books.find().sort( { title : 1 } )


Quiz: Query Language: Cursors


Q. > Recall the documents in the scores collection:
{
                "_id" : ObjectId("50844162cb4cf4564b4694f8"),
                "student" : 0,
                "type" : "exam",
                "score" : 75
}
Write a query that retrieves documents of type "exam", sorted by score in descending order, skipping the first 50 and showing only the next 20.


Ans 


db.scores.find( { type : "exam" } ).sort( { score : -1 } ).skip( 50 ).limit( 20 )




Homework: 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?



Ans.

16777216




Homework: 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?



 Ans.
 3



Homework: 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”?



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




Homework: 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.)


1. var c = db.products.find( { } ).sort( { name : -1 } ); while( c.hasNext() ) { print( c.next().name); }
2. var c = db.products.find( { }, { name : 1, _id : 0 } ).sort( { name : 1 } ); while( c.hasNext() ) { print( c.next().name); }
3. var c = db.products.find( { } ).sort( { name : 1 } ); c.forEach( function( doc ) { print( doc.name ) } );
4. db.products.find( { }, { name : 1, _id : 0 } ).sort( { name : 1 } )




 Ans.

2. var c = db.products.find( { }, { name : 1, _id : 0 } ).sort( { name : 1 } ); while( c.hasNext() ) { print( c.next().name); }
3. var c = db.products.find( { } ).sort( { name : 1 } ); c.forEach( function( doc ) { print( doc.name ) } );
4. db.products.find( { }, { name : 1, _id : 0 } ).sort( { name : 1 } )








2 comments: