![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 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](https://www.tutorialspoint.com/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 - What does the following MongoDB command return?
db.posts.find( { likes : { $gt : 100 }, likes : { $lt : 200 } } );
A - Posts with likes greater than 100 but less than 200
B - Posts with likes greater than or equal to 100 but less than or equal to 200
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 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 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 - Consider that you have a collection called population which has fields state and city. Which of the following query will calculate the population grouped by state and city?
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 - 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 - When should we consider representing a one-to-many relationship in an embedded collection instead of separate collection?
A - When the many is very large
Answer : B
Explanation
If the many is very large, then we should create separate collection, else the document size would go on increasing.