M102 WEEK 2 QUIZ

QUIZ QUESTIONS AND ANSWERS

Question: How would you insert the document
{ x : 3 , y : 4 }
into the temperature collection for the current database? Type your answer here as you would type it into the mongo shell.

Answer: db.temperature.insert({“x”:3,”y”:4})

Question: Check all that are true about the _id field:

  1. It must be of type ObjectId()
  2. It must exist in every document (Ans)
  3. It can be changed
  4. It must be unique inside the collection (Ans)
  5. It is automatically indexed (Ans)

Question: What happens if you try to use db.collection.save(document) if the inserted document has no _id?

  1. It will assign the document a new _id of the type already used in the table and insert it.
  2. It will assign the document an exisisting _id field at random and overwrite that existing document.
  3. It will insert the document with no _id.
  4. It will infer on its own what you meant and do that.
  5. It will assign the document an ObjecId for its _id field, and then insert it. (Ans)
  6. It will throw an exception, as it cannot update a document that does not exist in the collection.

Question: Suppose we have a collection, “cars”, with one document preexisting:
{ “_id” : 100, “name” : “GTO”, “year” : 1969, “color” : “red” }
Which of the following statements would set the available field to 1?

  1. db.cars.update({_id:100},{$set:{available:1}}) (Ans)
  2. db.cars.update({_id:100,$set:{available:1}})
  3. db.cars.update({},{$inc:{available:true}})

Question: Suppose we have documents in the users collection of the form:
{
_id : ObjectId(“50897dbb9b96971d287202a9”),
name : “Jane”,
likes : [ “tennis”, “golf” ],
registered : false,
addr : {
city : “Lyon”,
country : “France”
}
}
How would we, in the mongo shell, delete all documents in the collection where city is “Lyon” and registered is false?

Answer: db.users.remove({“addr.city”:”Lyon”,”registered”:false})

Question: Which of the following are disadvantages to setting multi=false (as it is by default)?

  1. Updates on non-indexed query fields will take longer.
  2. Updates on indexed query fields will take longer.
  3. Updates that the user may have intended to match multiple documents will exit prematurely, after only one update. (Ans)
  4. Updates that match only one document will take longer.

Question: Suppose we have documents in the users collection of the form:
{
_id : “Jane”,
likes : [ “tennis”, “golf” ]
}
How would we, in the mongo shell, add that this user likes “football”? We want to record this even if the user does not yet have a document. We also want to avoid having duplicate items in the “likes” field.
NOTE: Make sure to use the new syntax, which uses an options_document:
db.collection.update( query_document, update_document, options_document )

Answer: db.users.update({“_id”:”Jane”},{“$addToSet”: {“likes” :”football”}},{“upsert”:true})

Question: What are the basic building blocks of the wire protocol? Check all that apply.

  1. GetMore (Ans)
  2. Insert (Ans)
  3. Query (Ans)
  4. Remove (Ans)
  5. Update (Ans)
  6. Command

Question: Which operation is overloaded in the wire protocol to handle commands?

  1. GetMore
  2. Remove
  3. Query (Ans)
  4. Insert
  5. Update

Question: Which of the following are user commands (as opposed to admin commands)? Check all that apply.

  1. count (Ans)
  2. aggregate (Ans)
  3. drop
  4. MapReduce (Ans)
  5. addShard
  6. FindAndModify (Ans)
  7. serverStatus

Question: When we execute an operation through the shell or another interface, we can use which of the following?

  1. db.teaEarlGreyHot()
  2. db.engage()
  3. db.runCommand() (Ans)
  4. db.makeItSo()

Question: What does the db.isMaster() command do?

  1. Determine your level of mastery of the MongoDB shell
  2. Determine if this shard is the Primary shard
  3. Determine if this member of the replica set is Primary (Ans)
  4. Determine if the database has Master-Slave replication

Question: When running the command db.serverStatus() in the shell, what does the “ok” field represent?

  1. That the command was sent and received properly by the mongod (Ans)
  2. That the master db process is ok
  3. That the shell is running properly
  4. That the daemon to which you are connected is ok
  5. That the daemon has master status

Question: If you’re looking for problems with database performance, what is a good place to look, when you run db.currentOp()?

  1. “ok”:false
  2. “secs_running”:{look for long times} (Ans)
  3. “secs_running”:{look for short times}
  4. “opid”, because you always want to killOp() everything to clean things up.
  5. “opid”:(something that is not sequential with the rest)

Question: True or false: db.collection.remove({}), which removes all messages in a collection, is the same as db.collection.drop(), which drops the collection.

  • True
  • False (Ans)

Question: Which of these statements is true?

  1. MongoDB commands are for administrator use only
  2. In MongoDB collections are usually created implicitly (Ans)
  3. In MongoDB indexes are usually created implicitly

 

Leave a Reply

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