MongoDB Online Quiz


Following quiz provides Multiple Choice Questions (MCQs) related to MongoDB Framework. You will have to read all the given answers and click over the correct answer. If you are not sure about the answer then you can check the answer using Show Answer button. You can use Next Quiz button to check new set of questions in the quiz.

Questions and Answers

Q 1 - Consider a collection posts which has fields: _id, post_text, post_author, post_timestamp, post_tags etc. Which of the following query retrieves ONLY the key named post_text from the first document retrieved?

A - db.posts.find({},{_id:0, post_text:1})

B - db.posts.findOne({post_text:1})

C - db.posts.finOne({},{post_text:1})

D - db.posts.finOne({},{_id:0, post_text:1})

Answer : D


By default, MongoDB returns the _id field with each document. So in case you want ONLY the post_text field, you will have to exclude the _id field explicitly. Also, since we have to retrieve only the first document we have to use findOne and not find.

Q 2 - Which of the following replica sets vote in the election of a primary replica set?

A - Secondary

B - Hidden

C - Delayed

D - All of the above

Answer : D


All members of a replica set, unless the value of votes is equal to 0, vote in elections. This includes all delayed, hidden and secondary-only members.

Q 3 - Which of the following operations on a single document will operate atomically?

A - update

B - $push

C - Both a and b

D - None of the above

Answer : C


Both the update and $push operators will operate in an atomic way.

Answer : A


When you specify upsert: true for an update operation and no matching documents are found, MongoDB creates a new document.

Q 5 - You can implement a multi-document transaction in MongoDB using which of the following concept?

A - Tailable Cursor

B - Two Phase Commits

C - Compound Indexing

D - Multi Document Transaction is not supported by MongoDB

Answer : B


Operations on a single document are always atomic with MongoDB databases; however, operations that involve multiple documents, which are often referred to as “multi-document transactions”, are not atomic.

Answer : B


Pipeline stages have a limit of 100 megabytes of RAM. If a stage exceeds this limit, MongoDB will produce an error. If the aggregate command returns a single document that contains the complete result set, the command will produce an error if the result set exceeds the BSON Document Size limit, which is currently 16 megabytes.

Q 7 - In a sharded replicas set environment with multiple mongos servers, which of the following would decide the mongos failover?

A - mongo shell

B - mongod

C - individual language drivers

D - mongos

Answer : C


Since the mongos are itself failing in this case, this logic has to be built on the drivers side.

Answer : A


MongoDB cannot create a unique index on the specified index field(s) if the collection already contains data that would violate the unique constraint for the index. The syntax for the same is db.collection.createIndex( { a: 1 }, { unique: true } )

Q 9 - Consider that you have the following two documents in the products collection:

{ "_id" : 1, "prices" : [ 60, 100, 200 ] }

{ "_id" : 2, "prices" : [ 20, 90, 150 ] }

What will the following query result into:
   { _id: 1, prices: 100 },
   { $set: { "prices.$" : 111 } }

A - Updates 60 to 100

B - Updates 100 to 111

C - Updates 60,100 and 200 to 111

D - Removes the three elements of the prices array and replaces it with only a single element 111

Answer : B


The positional $ operator identifies an element in an array to update without explicitly specifying the position of the element in the array. To project, or return, an array element from a read operation, see the $ projection operator.

Q 10 - The _______ operator can be used to identify an element in the array to be updated without explicitly specifying the position of the element.

A - $

B - $elemMatch

C - $slice

D - Updating an array field without knowing its index is not possible.

Answer : A


The positional $ operator identifies an element in an array to update without explicitly specifying the position of the element in the array.