MongoDB Mock Test



This section presents you various set of Mock Tests related to MongoDB Framework. You can download these sample mock tests at your local machine and solve offline at your convenience. Every mock test is supplied with a mock test key to let you verify the final score and grade yourself.

Questions and Answers

MongoDB Mock Test II

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 2 - Consider that our posts collection contains an array field called tags that contains tags that the user enters.

{
            _id: 1,
            tags: [“tutorial”, “fun”, “learning”],
            post_text: “This is my first post”,	
            //other elements of document  	
} 

Which of the following commands will find all the posts that have been tagged as tutorial.

A - db.posts.find( { tags : "tutorial" } );

B - db.posts.find( { tags : ["tutorial"] } );

C - db.posts.find( { $array : {tags: "tutorial"} } );

D - db.posts.findInArray( { tags : "tutorial" } );

Answer : A

Explanation

Searching an array is no different than searching a normal field. Hence the first option.

Answer : A

Explanation

The efficiency of a MongoDB database depends on how well the database is designed depending on the application usage. The schema should be designed based on how the data access patterns are.

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

Explanation

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

Answer : C

Explanation

MongoDB does not support things like ACID Transactions or atomicity across collections or relationships like SQL.

Q 6 - Consider that our posts collection contains an array field called tags that contains tags that the user enters.

{
            _id: 1,
            tags: [“tutorial”, “fun”, “learning”],
            post_text: “This is my first post”,	
            //other elements of document  	
}

What does the following command return:

db.posts.find( { 'tags.0': “tutorial” } )

A - All the posts whose tags array contains tutorial

B - All the posts which contains only one tag element in the tag array

C - All the posts having the first element of the tags array as tutorial

D - All the posts which contains 0 or more tags named tutorial

Answer : C

Explanation

tags.0 means that the 0th element of the tag. This is the specific matching of an element in array.

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

Explanation

$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 8 - 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.

Answer : B

Explanation

{multi:true} should be used for this purpose. By default, MongoDB will update only the first document.

Q 10 - What does the following query do when performed on the posts collection?

db.posts.update({_id:1},{Title:This is post with ID 1"})

A - Updates the Title of the post

B - Updating a document is possible only with $set

C - Replaces the complete document with _id as 1 with the document specified in second parameter

D - Syntax error

Answer : C

Explanation

Updating a document without using $set replaces the entire document with whatever document is specified in second parameter.

Answer : D

Explanation

$set sets the specific fields in the matched documents or adds them as a new field if not already present.

Answer : A

Explanation

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

Q 13 - Consider that you are using { upsert : true } option in your update command. Which of the following parameters will be used to determine if any new documents were inserted:

A - nMatched

B - nInserted

C - nModified

D - nUpserted

Answer : D

Explanation

The nUpserted shows the number of documents that were added during the update operation.

Q 14 - 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.

Answer : C

Explanation

You cannot insert two documents with the same _id field. The value of _id field should be unique.

Q 16 - Which option should be used with findAndModify() command to return the modified document instead of the pre-modification document?

A - findAndModify by default returns the pre-modification document

B - Set {new : true}

C - Use the POST version of findAndModify called findAndModifyPost

D - Both b and c are valid

Answer : B

Explanation

When true, returns the modified document rather than the original. The findAndModify() method ignores the new option for remove operations. The default is false.

Q 17 - 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 18 - 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 19 - Which of the following operator can be used to limit the number of documents in an array field of a document after an update is performed?

A - $push along with $each, $sort and $slice

B - $removeFromSet

C - $arrayLimit

D - None of the above

Answer : A

Explanation

You can iterate over all the array elements using $each, slice them using $slice and then push them back to the document using $push.

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

A - cursor.next()

B - cursor.hasNext()

C - cursor.forEach()

D - All of the above

Answer : D

Explanation

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

Q 21 - Which language is MongoDB written in?

A - C++

B - Java

C - Python

D - MongoC

Answer : A

Explanation

MongoDB is written in C++.

Q 22 - 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.

Answer : B

Explanation

explain.executionStats.totalKeysExamined indicates the number of index entries scanned.

Q 24 - If the value of totalKeysExamined is 30000 and the value of totalDocsExamined is 0, which of the following option is correct?

A - The query used an index to fetch the results

B - The query returned 30000 documents after scanning the documents

C - The query returned 0 documents

D - None of the above

Answer : A

Explanation

When an index covers a query, the explain result has an IXSCAN stage that is not a descendant of a FETCH stage, and in the executionStats, the totalDocsExamined is 0.

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

Explanation

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 Sheet

Question Number Answer Key
1 C
2 A
3 A
4 C
5 C
6 C
7 A
8 B
9 B
10 C
11 D
12 A
13 D
14 D
15 C
16 B
17 A
18 D
19 A
20 D
21 A
22 D
23 B
24 A
25 B
mongodb_questions_answers.htm
Advertisements