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.

Answer : A


The $gt, $lt and related operators can be applied for string manipulations too. They work in the same manner as they would work on numeric values.

Q 4 - Consider that the posts collection contains an array called ratings which contains ratings given to the post by various users in the following format:

         _id: 1,
         post_text: “This is my first post”,
         ratings: [5, 4, 2, 5],
         //other elements of document 			

Which of the following query will return all the documents where the array ratings contains at least one element between 3 and 6?

A - db.inventory.find( { ratings: { $elemMatch: { $gt: 3, $lt: 6 } } } )

B - db.inventory.find( { ratings: { ratings: { $gt: 5, $lt: 9 } } } )

C - db.inventory.find( { ratings: { ratings.$: { $gt: 5, $lt: 9 } } } )

D - db.inventory.find( { ratings: { $elemMatch: { $gte: 3, $lte: 6 } } } )

Answer : A


$elemMatch operator is used to specify multiple criteria on the elements of an array such that at least one array element satisfies all the specified criteria.

Q 5 - Which of the following methods can be used on a cursor object?

A -

B - cursor.hasNext()

C - cursor.forEach()

D - All of the above

Answer : D


All of the above methods can be used on a cursor object.

Q 6 - What is the equivalent command in MongoDB for the following SQL query?

SELECT * FROM posts WHERE author like "%john%"

A - db.posts.find( { author: /john/ } )

B - db.posts.find( { author: {$like: /john/} } )

C - db.posts.find( { $like: {author: /john/} } )

D - db.posts.find( { author: /^john^/ } )

Answer : A


db.posts.find( { author: /john/ } )

Answer : A


The above query basically matches all the documents having likes between 100 and 200. After that, it just specifies that aggregation is not to be done with any specific column (_id:null) and increments the count every time. Thus calculating the total such posts.

Q 8 - Which of the following aggregate commands in MongoDB uses a pipeline approach with the goals of improving the aggregation performance?

A - aggregate

B - mapReduce

C - group

D - All of the above

Answer : A


The aggregate command in MongoDB is designed with specific goals of improving performance and usability for aggregation tasks. It uses a “pipeline” approach where objects are transformed as they pass through a series of pipeline operators such as $group, $match, and $sort.

Q 9 - Which format/standard is used by MongoDB internally to store documents?



C - JSON - Extended

D - B+ Trees

Answer : A


MongoDB uses BSON, a binary object format similar to, but more expressive than JSON.

Q 10 - When should we consider representing a one-to-many relationship in an embedded collection instead of separate collection?

A - When the many is very large

B - When the many is not very large

C - Never

D - Always

Answer : B


If the many is very large, then we should create separate collection, else the document size would go on increasing.



We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.