MongoDB Online Quiz


Advertisements

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 - 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

Explanation

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 : C

Explanation

When the mongo shell interprets this query, it will override the first condition $gt and consider only the $lt one. To apply both the less than and greater than condition, you will have to use the $and operator.

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 ratings array contains elements that in some combination satisfy the query conditions?

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 : B

Explanation

This query will check if the array elements match the given condition in some or the other way or combination.

Q 5 - Which are the ONLY ways to project portions of an array?

A - $elemMatch

B - $slice

C - $

D - All of the above

Answer : D

Explanation

$elemMatch, $slice, and $ are the only way to project portions of an array. For e.g. you cannot use {“tags.0”:1} for projection.

Q 6 - Update If Correct is an approach for which of the following concepts in MongoDB:

A - Concurrency Control

B - Transaction Management

C - Atomicity

D - Performance Management

Answer : A

Explanation

The Update if Current pattern is an approach to concurrency control when multiple applications have access to the data.

Q 7 - In a sharded replica set environment, the w Option provides ability for write concern and j Option provides ability for the data to be written on disk journal. Consider that we have a seven member replica set and we want to assure that the writes are committed to journal. What should be the value of j?

A - 0

B - 1

C - 2

D - 7

Answer : B

Explanation

To enable the disk journaling commits, j value should always be set to 1.

Q 8 - Which of the following aggregation commands in MongoDB does not support sharded collections?

A - aggregate

B - mapReduce

C - group

D - All of the above

Answer : C

Explanation

The group command in MongoDB groups documents in a collection by the specified key and performs simple aggregation functions, such as computing counts and sums. It is the most basic one and does not support such sharding concepts.s

Q 9 - Which of the following operators can reverse the effects of a double unwind operation?

A - $push

B - $wind

C - $wind.$wind

D - Can’t be reversed.

Answer : A

Explanation

An unwind operation unwinds on an array field and creates separate documents. If you unwind it again same thing happens again. So if you had one document which had two arrays, the first array had 2 values and second array has 3 values. Unwinding this document two times will give 6 document. Now to combine them back, you can use the $push operator.

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