![MongoDB Tutorial](https://www.tutorialspoint.com/mongodb/images/mongodb-mini-logo.jpg)
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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](https://www.tutorialspoint.com/images/online_mock_tests.png)
MongoDB Mock Test I
Q 1 - What kind of database MongoDB is?
Answer : B
Explanation
MongoDB stores data in JSON structure based documents. These documents in turn contains data in form of key value pairs.
Q 2 - A collection and a document in MongoDB is equivalent to which of the SQL concepts respectively?
Answer : A
Explanation
The way SQL databases stores data rows in a table, MonngoDB stores documents inside collections.
Q 3 - Which of the following is correct about MongoDB?
A - MongoDB uses JSON format to represent documents
B - MongoDB supports collection joins
Answer : D
Explanation
MongoDB provides specific supports for functionalities related to 2d and 3d geospatial problems.
Q 4 - Which of the following is a valid MongoDB JSON document:
B
{
"user_id"=1,
"user_name"="Joe Sanders",
"occupation"=["engineer","writer"]
}
C
{
"user_id":1;
"user_name":"Joe Sanders";
"occupation":["engineer","writer"]
}
Answer : A
Explanation
A blank document is valid in MongoDB. However, rest of the three documents have some or the other problem. Option b has “=”, Option c has “;” and Option d has an incorrect array format. It should be a sub-document instead.
Q 5 - Which of the following is correct explanation of MongoDB processes?
A - mongod.exe is the shell process and mongo.exe is the actual database process
B - mongo.exe is the shell process and mongod.exe is the actual database process
C - mongos.exe is the MongoDB server process needed to run database
D - mongodump.exe can be used to import database backup dump
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.
Q 6 - 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})
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 7 - Which of the following is incorrect statement about find and findOne operations in MongoDB?
A - find() returns all the documents in a collection while findOne() retrieves only the first one.
B - find() and findOne() returns cursors to the collection documents
C - findOne() returns the actual first document retrieved from a collection
Answer : B
Explanation
Both findOne() and find() queries are very much different. The find() method returns the cursor while the findOne() returns the actual document. Hence Option b is incorrect and rest of them are correct.
Q 8 - In a collection that contains 100 post documents, what does the following command do?
db.posts.find().skip(5).limit(5)
A - Skip and limit nullify each other. Hence returning the first five documents.
B - Skips the first five documents and returns the sixth document five times
C - Skips the first five documents and returns the next five
D - Limits the first five documents and then return them in reverse order
Answer : C
Explanation
The skip and limit functions are applies linearly and hence it will first skip documents 1-5, and then return documents 6-10.
Q 9 - Which of the following MongoDB query is equivalent to the following SQL query:
UPDATE users SET status = "C" WHERE age > 25
A
db.users.update(
{ age: { $gt: 25 } },
{ status: "C" })
B
db.users.update(
{ age: { $gt: 25 } },
{ $set: { status: "C" } })
C
db.users.update(
{ age: { $gt: 25 } },
{ $set: { status: "C" } },
{ multi: true })
D
db.users.update(
{ age: { $gt: 25 } },
{ status: "C" },
{ multi: true })
Answer : C
Explanation
$set is used to set the value of a particular field in a document. The syntax of set is $set:{column_name : column_value}. Also, {multi:true} is needed to update all the documents. Otherwise only the first found document is updated.
Q 10 - The MongoDB explain() method does not support which of the following verbosity mode:
Answer : D
Explanation
The possible modes of explain() are: "queryPlanner", "executionStats", and "allPlansExecution".
Q 11 - Which is the default mode in which the explain() command runs?
Answer : A
Explanation
Default mode is "queryPlanner".
Q 12 - Within how much time does MongDB writes are written to the journal?
Answer : B
Explanation
Writes are physically written to the journal within 100 milliseconds, by default.
Q 13 - Which of the following is true about sharding?
A - Sharding is enabled at the database level
B - Creating a sharded key automatically creates an index on the collection using that key
C - We cannot change a shard key directly/automatically once it is set up
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 14 - What is the maximum size of a MongoDB document?
Answer : B
Explanation
The maximum BSON document size is 16 megabytes. The maximum document size helps ensure that a single document cannot use excessive amount of RAM or, during transmission, excessive amount of bandwidth.
Q 15 - What is the maximum size of Index Key Limit and Number of Indexes per collection?
Answer : C
Explanation
The total size of an index entry, which can include structural overhead depending on the BSON type, must be less than 1024 bytes. A single collection can have no more than 64 indexes.
Answer : A
Explanation
In the default configuration, MongoDB writes data to the main data files on disk every 60 seconds.
Q 17 - Which of the following commands finds all the documents in the posts collection with post timestamp field as null?
A - db.posts.find( { post_timestamp : { $type: 10 } } )
B - db.posts.find( { post_timestamp: { $type: null } } )
C - db.posts.find( { post_timestamp: { $fieldtype: 10 } } )
D - db.posts.find( { post_timestamp: { $fieldtype: null } } )
Answer : A
Explanation
$type is used for all the operations involving checking the type of a field in MongoDB. 10 represents the BSON value for null.
Q 18 - mongoimport command is used to:
A - import all the data from one database to another
B - import all the data from one collection to another
C - imports content from an Extended JSON, CSV, or TSV export created by mongoexport
Answer : C
Explanation
The mongoimport tool imports content from an Extended JSON, CSV, or TSV export created by mongoexport, or potentially, another third-party export tool.
Q 19 - Which of the following command can be used in mongo shell to show all the databases in your MongoDB instance?
Answer : A
Explanation
show dbs returns the list of all the databases.
Q 20 - Which of the following replica sets vote in the election of a primary replica set?
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.
Q 21 - Which of the following command can be used to check the size of a collection named posts?
Answer : A
Explanation
To view the statistics for a collection, including the data size, use the db.collection.stats() method from the mongo shell.
Q 22 - Which of the following commands can cause the database to be locked?
Answer : D
Explanation
All the above commands wither result in a read lock or a write lock or both.
Q 23 - 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?
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 24 - Which of the following commands will return all the posts with number of likes greater than 100 and less than 200, both inclusive?
A - db.posts.find({ likes : { $gt : 100, $lt : 200 } } );
B - db.posts.find({ likes : { $gte : 100, $lt : 200 } } );
C - db.posts.find({ likes : { $gt : 100 , $lte : 200 } } );
D - db.posts.find({ likes : { $gte : 100 , $lte : 200 } } );
Answer : D
Explanation
Since 100 and 200 are both inclusive, we need $gte (greater than and equal) and $lte (less than and equal).
Q 25 - In our posts collection, which command can be used to find all the posts whose author names begin lie between “A” and “C” in dictionary order?
A - db.posts.find( { post_author : { $gte : "A" , $lte : "C" } } );
B - db.posts.find( { post_author : { $gte : "C" , $lte : "A" } } );
C - db.posts.find( { post_author : { $gt : "A" , $lt : "C" } } );
Answer : A
Explanation
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.
Answer Sheet
Question Number | Answer Key |
---|---|
1 | B |
2 | A |
3 | D |
4 | A |
5 | B |
6 | D |
7 | B |
8 | C |
9 | C |
10 | D |
11 | A |
12 | B |
13 | C |
14 | B |
15 | C |
16 | A |
17 | A |
18 | C |
19 | A |
20 | D |
21 | A |
22 | D |
23 | A |
24 | D |
25 | A |