![MongoDB Tutorial](/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
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](/images/questions_and_answers.png)
Q 1 - 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 2 - 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 3 - Which of the following is the most important consideration while designing the schema for MongoDB?
A - The schema should match the data access and query patterns
B - The schema should be kept in 3NF similar to SQL schemas
C - The schema should focus on creating possible embedded documents
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 - 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 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
C - Use the POST version of findAndModify called findAndModifyPost
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 6 - Which is the correct order (lowest to highest) in which MongoDB compares the BSON types?
A - Null, Number, String and Object
B - Number, Null, String and Object
Answer : A
Explanation
This is the defined order in which the bson types are compared. There are various other fields as per the BSON specification which can be found here: http://docs.mongodb.org/manual/reference/bson-types/
Q 7 - In a sharded replicas set environment with multiple mongos servers, which of the following would decide the mongos failover?
Answer : C
Explanation
Since the mongos are itself failing in this case, this logic has to be built on the drivers side.
Q 8 - Given the following posts document:
{ "_id" : 1, "post_text" : "This post does not matter”, “tags”: [ "tutorial", "fun", "learning"], // rest of the document }
What will be the output of following query:
db.posts.aggregate( [ { $unwind : "$tags" } ] )
A - Return three separate documents for three separate tags
B - Arranges the tags (wind) in ascending order
C - Arranges the tags (wind) in descending order
D - Returns one document but converts the tags array in an object
Answer : A
Explanation
Deconstructs an array field from the input documents to output a document for each element. Each output document is the input document with the value of the array field replaced by the element.
Q 9 - Which of the following operators can reverse the effects of a double unwind operation?
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.
Q 10 - Which index is used to index the content stored in arrays?
Answer : A
Explanation
MongoDB uses multikey indexes to index the content stored in arrays. If you index a field that holds an array value, MongoDB creates separate index entries for every element of the array.