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

Answer : B

Explanation

The core components in the MongoDB package are: mongod, the core database process; mongos the controller and query router for sharded clusters; and mongo the interactive MongoDB Shell.

Answer : C

Explanation

There is no direct way of changing the sharded key unless you dump the entire data, drop the sharded key and then re-import everything. Other all options are false. Sharding is enabled at collection level, it does not create any index by default and finally sharding environment supports regular sorting.

Q 3 - Which of the following commands can cause the database to be locked?

A - Issuing a query

B - Inserting data

C - Map-reduce

D - All of the above

Answer : D

Explanation

All the above commands wither result in a read lock or a write lock or both.

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 - Consider the following posts document:

{
 	_id: 1,
	post_text: “This is my first post”,
	author: “Tom”,
	tags: [“tutorial”,”quiz”,”facebook”,”learning”,”fun”]
}

Which of the following queries will return the documents but with only the first two tags in the tags array?

A - db.posts.find({author:"Tom"},{tags:{$slice:2}})

B - db.posts.find({author:"Tom"}).limit({tags:2})

C - db.posts.find({author:"Tom"}).limit($slice:{tags:2})

D - Both a and c are valid. $slice works both with projection and limit.

Answer : A

Explanation

The $slice operator controls the number of items of an array that a query returns.

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

Explanation

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

Answer : A

Explanation

You have to give state and city as the key to group by and then calculate the sum of the population in each city.

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

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:
db.products.update(
   { _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

Explanation

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 - Consider the following document from the products collection:

{
 _id: 1,
 product_code: "345678",
 variations: [
              { size: "L", price: 1000 },
              { size: "M", price: 800 }
           ]
}

What does the following query using $elemMatch return?

db.products.find( { product_code: "345678" },
                 { variations: { $elemMatch: { size: “L” } } } )

A - Returns the complete document since MongoDB does not support partial array retrieval

B - Returns the document but with only one element in the variations array (corresponding to size L)

C - Returns the complete document but retrieves only the size field from the array

D - Returns the complete document but retrieves only the size field from the array and also with only one element in the variations array (corresponding to size L)

Answer : B

Explanation

The $elemMatch operator limits the contents of an <array> field from the query results to contain only the first element matching the $elemMatch condition.

mongodb_questions_answers.htm
Advertisements