## Implementation Using JAVA DRIVER
======================================
@NOTE: new BasicDBObject() is same as '{}' in mongo shell...given you have a hands-on exp of querrying over mongo-shell ,Keeping this in mind will help a lot while writing Code for JAVA DRIVER to query mongoDB.
# Java Driver: Representing Documents
How would you create a document using the Java driver with this JSON structure:
{
"_id" : "user1",
"interests" : [ "basketball", "drumming"]
}
new HashMap().put("_id", "user1").put(Arrays.asList("basketball", "drumming"));
new BasicDBObject("_id", "user1").append("interests", "basketball", "drumming");
new DBObject("_id", "user1").append("interests", Arrays.asList("basketball", "drumming"));
new BasicDBObject("_id", "user1").append("interests", Arrays.asList("basketball", "drumming")); [# CORRECT #]
------------------------------------------------------------------------------------------------------------
# Java Driver: Insert
Do you expect the second insert below to succeed?
MongoClient client = new MongoClient();
DB db = client.getDB("school");
DBCollection people = db.getCollection("people");
DBObject doc = new BasicDBObject("name", "Andrew Erlichson")
.append("company", "10gen");
try {
people.insert(doc); // first insert
doc.removeField("_id"); // remove the "_id" field
people.insert(doc); // second insert
} catch (Exception e) {
e.printStackTrace();
}
No, because the _id will be a duplicate in the collection
No, because the removeField call will remove the entire document
Yes, because the removeField call will remove the _id key added by the driver in the first insert [# CORRECT #]
Yes, because the driver always adds a unique _id field on insert.
------------------------------------------------------------------------------------------------------------
# Java Driver: find, findOne, count : 'collection.find(), collection.findOne() , collection.count()'
In the following code snippet:
MongoClient client = new MongoClient();
DB db = client.getDB("school");
DBCollection people = db.getCollection("people");
DBObject doc;
xxxx
System.out.println(doc);
Please enter the simplest one line of Java code that would be needed in place of xxxx to make it print one document from the people collection.
> doc = people.findOne(); [# ANSWER #]
------------------------------------------------------------------------------------------------------------
# Java Driver: Query Criteria
Given a collection of documents with two fields -- type and score -- what is the correct line of code to find all documents where type is "quiz" and score is greater than 20 and less than 90. Select all that apply.
scores.find(new BasicDBObject("score", new BasicDBObject("$gt", 20).append("$lt", 90));
scores.find(new BasicDBObject("type", "quiz").append("score", new BasicDBObject("$gt", 20).append("$lt", 90))) [# CORRECT #]
scores.find(new BasicDBObject("type", "quiz").append("$gt", new BasicDBObject("score", 20)).append("$lt", new BasicDBObject("score", 90))) scores.find(QueryBuilder.start("type").is("quiz").and("score").greaterThan(20).lessThan(90).get()) [# CORRECT #]
------------------------------------------------------------------------------------------------------------
# Java Driver: field Selection
------------------------------------------------------------------------------------------------------------
# Java Driver: Dot Notation
In the following code snippet, what do you think will happen if there exists in the collection a document that matches the query but does not have a key called "media.url"?
DBObject findOneUrlByMediaType(DBCollection videos, String mediaType) {
DBObject query = new BasicDBObject("media.type", mediaType);
DBObject projection = new BasicDBObject("media.url", true);
return videos.findOne(query, projection);
}
It will throw an exception
It will return an empty document
It will return a document containing a single field containing the document's _id [# CORRECT #]
There is not enough information to know
------------------------------------------------------------------------------------------------------------
# Java Driver: Sort, Skip and Limit
Supposed you had the following documents in a collection named things.
{ "_id" : 0, "value" : 10 }
{ "_id" : 1, "value" : 5 }
{ "_id" : 2, "value" : 7 }
{ "_id" : 3, "value" : 20 }
If you performed the following query in the Java driver:
collection.find().sort(new BasicDBObject("value", -1)).skip(2).limit(1);
which document would be returned?
The document with _id=0
The document with _id=1
The document with _id=2
The document with _id=3
-----------------------------------------------------------------------------------------------------------
# Java Driver: Update and Remove
In the following code fragment, what is the Java expression in place of xxxx that will set the field "examiner" to the value "Jones" for the document with _id of 1. Please use the $set operator.
# update using $set
scores.update(new BasicDBObject("_id", 1), xxxx);
new BasicDBObject("$set",new BasicDBObject("examiner","Jones"))
-----------------------------------------------------------------------------------------------------------
# Java Driver -- findAndModify();
------------------------------------------------------------------------------------------------------------
## MongoDB Schema Design
=======================
What's the single most important factor in designing your application schema within MongoDB?
Making the design extensible.
Making it easy to read by a human.
Matching the data access patterns of your application. [# CORRECT #]
Keeping the data in third normal form.
----------------------------------------------------------------------------------------------------------
Living Without Transactions
Which of the following operations operate atomically within a single document? Check all that apply.
Update [# CORRECT #]
findAndModify [# CORRECT #]
$addToSet(within an update) [# CORRECT #]
$push within an update [# CORRECT #]
------------------------------------------------------------------------------------------------------------
# One to One Relations
What's a good reason you might want to keep two documents that are related to each other one-to-one in separate collections? Check all that apply.
Because you want to allow atomic update of both documents at once.
To reduce the working set size of your application. [# CORRECT #]
To enforce foreign key constraints
Because the combined size of the documents would be larger than 16MB [# CORRECT #]
# One to Many Relations
When is it recommended to represent a one to many relationship in multiple collections?
Always
Whenever the many is large
Whenever the many is actually few Never
------------------------------------------------------------------------------------------------------------
# Trees
Given the following typical document for a e-commerce category hierarchy collection called categories
{
_id: 34,
name : "Snorkeling",
parent_id: 12,
ancestors: [12, 35, 90]
}
Which query will find all descendants of the snorkeling category?
db.categories.find({ancestors:{'$in':[12,35,90]}})
db.categories.find({parent_id: 34})
db.categories.find({_id:{'$in':[12,35,90]}})
db.categories.find({ancestors:34}) [# CORRECT #]
-----------------------------------------------------------------------------------------------------------
# Handling Blobs
Which of the following statements are true about GridFS?
GridFS stores large blobs in a single collection by breaking up the file into multiple pieces.
Each gridFS document is given a unique filename.
GridFS stores large blobs in two collections, one for metadata and one for the blob chunks. [# CORRECT #]
GridFS compresses your file on disk.
======================================
@NOTE: new BasicDBObject() is same as '{}' in mongo shell...given you have a hands-on exp of querrying over mongo-shell ,Keeping this in mind will help a lot while writing Code for JAVA DRIVER to query mongoDB.
# Java Driver: Representing Documents
How would you create a document using the Java driver with this JSON structure:
{
"_id" : "user1",
"interests" : [ "basketball", "drumming"]
}
new HashMap().put("_id", "user1").put(Arrays.asList("basketball", "drumming"));
new BasicDBObject("_id", "user1").append("interests", "basketball", "drumming");
new DBObject("_id", "user1").append("interests", Arrays.asList("basketball", "drumming"));
new BasicDBObject("_id", "user1").append("interests", Arrays.asList("basketball", "drumming")); [# CORRECT #]
------------------------------------------------------------------------------------------------------------
# Java Driver: Insert
Do you expect the second insert below to succeed?
MongoClient client = new MongoClient();
DB db = client.getDB("school");
DBCollection people = db.getCollection("people");
DBObject doc = new BasicDBObject("name", "Andrew Erlichson")
.append("company", "10gen");
try {
people.insert(doc); // first insert
doc.removeField("_id"); // remove the "_id" field
people.insert(doc); // second insert
} catch (Exception e) {
e.printStackTrace();
}
No, because the _id will be a duplicate in the collection
No, because the removeField call will remove the entire document
Yes, because the removeField call will remove the _id key added by the driver in the first insert [# CORRECT #]
Yes, because the driver always adds a unique _id field on insert.
------------------------------------------------------------------------------------------------------------
# Java Driver: find, findOne, count : 'collection.find(), collection.findOne() , collection.count()'
In the following code snippet:
MongoClient client = new MongoClient();
DB db = client.getDB("school");
DBCollection people = db.getCollection("people");
DBObject doc;
xxxx
System.out.println(doc);
Please enter the simplest one line of Java code that would be needed in place of xxxx to make it print one document from the people collection.
> doc = people.findOne(); [# ANSWER #]
------------------------------------------------------------------------------------------------------------
# Java Driver: Query Criteria
Given a collection of documents with two fields -- type and score -- what is the correct line of code to find all documents where type is "quiz" and score is greater than 20 and less than 90. Select all that apply.
scores.find(new BasicDBObject("score", new BasicDBObject("$gt", 20).append("$lt", 90));
scores.find(new BasicDBObject("type", "quiz").append("score", new BasicDBObject("$gt", 20).append("$lt", 90))) [# CORRECT #]
scores.find(new BasicDBObject("type", "quiz").append("$gt", new BasicDBObject("score", 20)).append("$lt", new BasicDBObject("score", 90))) scores.find(QueryBuilder.start("type").is("quiz").and("score").greaterThan(20).lessThan(90).get()) [# CORRECT #]
------------------------------------------------------------------------------------------------------------
# Java Driver: field Selection
------------------------------------------------------------------------------------------------------------
# Java Driver: Dot Notation
In the following code snippet, what do you think will happen if there exists in the collection a document that matches the query but does not have a key called "media.url"?
DBObject findOneUrlByMediaType(DBCollection videos, String mediaType) {
DBObject query = new BasicDBObject("media.type", mediaType);
DBObject projection = new BasicDBObject("media.url", true);
return videos.findOne(query, projection);
}
It will throw an exception
It will return an empty document
It will return a document containing a single field containing the document's _id [# CORRECT #]
There is not enough information to know
------------------------------------------------------------------------------------------------------------
# Java Driver: Sort, Skip and Limit
Supposed you had the following documents in a collection named things.
{ "_id" : 0, "value" : 10 }
{ "_id" : 1, "value" : 5 }
{ "_id" : 2, "value" : 7 }
{ "_id" : 3, "value" : 20 }
If you performed the following query in the Java driver:
collection.find().sort(new BasicDBObject("value", -1)).skip(2).limit(1);
which document would be returned?
The document with _id=0
The document with _id=1
The document with _id=2
The document with _id=3
-----------------------------------------------------------------------------------------------------------
# Java Driver: Update and Remove
In the following code fragment, what is the Java expression in place of xxxx that will set the field "examiner" to the value "Jones" for the document with _id of 1. Please use the $set operator.
# update using $set
scores.update(new BasicDBObject("_id", 1), xxxx);
new BasicDBObject("$set",new BasicDBObject("examiner","Jones"))
-----------------------------------------------------------------------------------------------------------
# Java Driver -- findAndModify();
------------------------------------------------------------------------------------------------------------
## MongoDB Schema Design
=======================
What's the single most important factor in designing your application schema within MongoDB?
Making the design extensible.
Making it easy to read by a human.
Matching the data access patterns of your application. [# CORRECT #]
Keeping the data in third normal form.
----------------------------------------------------------------------------------------------------------
Living Without Transactions
Which of the following operations operate atomically within a single document? Check all that apply.
Update [# CORRECT #]
findAndModify [# CORRECT #]
$addToSet(within an update) [# CORRECT #]
$push within an update [# CORRECT #]
------------------------------------------------------------------------------------------------------------
# One to One Relations
What's a good reason you might want to keep two documents that are related to each other one-to-one in separate collections? Check all that apply.
Because you want to allow atomic update of both documents at once.
To reduce the working set size of your application. [# CORRECT #]
To enforce foreign key constraints
Because the combined size of the documents would be larger than 16MB [# CORRECT #]
# One to Many Relations
When is it recommended to represent a one to many relationship in multiple collections?
Always
Whenever the many is large
Whenever the many is actually few Never
------------------------------------------------------------------------------------------------------------
# Trees
Given the following typical document for a e-commerce category hierarchy collection called categories
{
_id: 34,
name : "Snorkeling",
parent_id: 12,
ancestors: [12, 35, 90]
}
Which query will find all descendants of the snorkeling category?
db.categories.find({ancestors:{'$in':[12,35,90]}})
db.categories.find({parent_id: 34})
db.categories.find({_id:{'$in':[12,35,90]}})
db.categories.find({ancestors:34}) [# CORRECT #]
-----------------------------------------------------------------------------------------------------------
# Handling Blobs
Which of the following statements are true about GridFS?
GridFS stores large blobs in a single collection by breaking up the file into multiple pieces.
Each gridFS document is given a unique filename.
GridFS stores large blobs in two collections, one for metadata and one for the blob chunks. [# CORRECT #]
GridFS compresses your file on disk.