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

Explanation

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 - What is the output of the following program?

A - 60 s

B - 100 ms

C - 1 s

D - 100 s

Answer : A

Explanation

In the default configuration, MongoDB writes data to the main data files on disk every 60 seconds.

Q 3 - By default, the MongoDB cursor in mongo shell is configured to return how many documents? To get the next set of documents, which command is used?

A - 20, it

B - 200, more

C - 50, it

D - No limit, none

Answer : A

Explanation

In the mongo shell, if the returned cursor is not assigned to a variable using the var keyword, then the cursor is automatically iterated up to 20 times [1] to print up to the first 20 documents in the results. To get the next set of results, you should use it command which will iterate over the next set of results.

Q 4 - Which of the following commands removes a single document that matches the condition that Author is Joe?

A - db.posts.removeOne( { Author : "Joe" }, 1 )

B - db.posts.remove( { Author : "Joe" }, 1 )

C - db.posts.remove( { Author : "Joe" }, {justOne: true} )

D - Both b and c

Answer : D

Explanation

The third parameter to the remove function is justOne. You can either simply say 1 or write justOne as true.

Q 5 - Which type of indexes does MongoDB support?

A - Compound Indexes

B - Multikey Indexes

C - Geospatial Indexes

D - All of the above

Answer : D

Explanation

MongoDB supports all of the above mentioned indexes.

Q 6 - For capped collection, cursors which do not automatically close and remain open after the client exhausts the results are called:

A - Capped Cursors

B - Tailable Cursors

C - Open Cursors

D - Indexing Cursors

Answer : B

Explanation

By default, MongoDB will automatically close a cursor when the client has exhausted all results in the cursor. However, for capped collections you may use a Tailable Cursor that remains open after the client exhausts the results in the initial cursor.

Q 7 - What does the output x of the following MongoDB aggregation query result into:

db.posts.aggregate( [ { $group: { _id: "$author", x: { $sum: $likes } } } ] )

A - Average of likes on all the posts of an author, grouped by author

B - Number of posts by an author

C - Sum of likes on all the posts by an author, grouped by author

D - Sum of likes on all the posts by all the authors

Answer : C

Explanation

The above query first does a grouping on author field and then calculates the number of likes on all the posts that were grouped together.

Q 8 - What is a replica set node which does not maintain its own data but exists only for voting purpose called?

A - Secondary

B - Arbiter

C - Delayed

D - Hidden

Answer : B

Explanation

We may add an extra mongod instance to a replica set as an arbiter. Arbiters do not maintain a data set. Arbiters only exist to vote in elections. If your replica set has an even number of members, add an arbiter to obtain a majority of votes in an election for primary. Arbiters do not require dedicated hardware

Answer : A

Explanation

In the aggregation pipeline, it is not necessary to have a $group or $project or $match before the $sort operation.

Answer : C

Explanation

If you need to rebuild indexes for a collection you can use the db.collection.reIndex() method to rebuild all indexes on a collection in a single operation. This operation drops all indexes, including the _id index, and then rebuilds all indexes.

mongodb_questions_answers.htm
Advertisements