- 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
- 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 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.
Q 1 - 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.
Answer : A
Explanation
In the default configuration, MongoDB writes data to the main data files on disk every 60 seconds.
Q 3 - 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 4 - 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 )
Answer : D
Explanation
The third parameter to the remove function is justOne. You can either simply say 1 or write justOne as true.
Q 5 - Which type of indexes does MongoDB support?
Answer : D
Explanation
MongoDB supports all of the above mentioned indexes.
Q 6 - For capped collection, cursors which do not automatically close and remain open after the client exhausts the results are called:
Answer : B
Explanation
By default, MongoDB will automatically close a cursor when the client has exhausted all results in the cursor. However, for capped collections you may use a Tailable Cursor that remains open after the client exhausts the results in the initial cursor.
Q 7 - What does the output x of the following MongoDB aggregation query result into:
db.posts.aggregate( [ { $group: { _id: "$author", x: { $sum: $likes } } } ] )A - Average of likes on all the posts of an author, grouped by author
B - Number of posts by an author
C - Sum of likes on all the posts by an author, grouped by author
Answer : C
Explanation
The above query first does a grouping on author field and then calculates the number of likes on all the posts that were grouped together.
Q 8 - What is a replica set node which does not maintain its own data but exists only for voting purpose called?
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 - Which of the following aggregation query will sort the posts collection with author key ascending:
A - db.posts.aggregate([ {$sort:{ author:1 } } ])
B - db.posts.aggregate([ {$sort:{ author:-1 } } ])
C - db.posts.aggregate([ {author: {$sort: 1} } ])
D - You need to first use $group or $project or $match command before $sort in the pipeline
Answer : A
Explanation
In the aggregation pipeline, it is not necessary to have a $group or $project or $match before the $sort operation.
Q 10 - Which command can be used to rebuild the indexes on a collection in MongoDB?
A - db.collection.createIndex({reIndex:1})
Answer : C
Explanation
If you need to rebuild indexes for a collection you can use the db.collection.reIndex() method to rebuild all indexes on a collection in a single operation. This operation drops all indexes, including the _id index, and then rebuilds all indexes.