- 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.
Q 1 - 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 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 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 4 - 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:
Answer : D
Explanation
The nUpserted shows the number of documents that were added during the update operation.
Q 5 - 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?
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 6 - Update If Correct is an approach for which of the following concepts in MongoDB:
Answer : A
Explanation
The Update if Current pattern is an approach to concurrency control when multiple applications have access to the data.
Q 7 - In a sharded replica set environment, the w Option provides ability for write concern and j Option provides ability for the data to be written on disk journal. Consider that we have a seven member replica set and we want to assure that the writes are committed to journal as well as acknowledged by at least 3 nodes. What should be the value of w?
Answer : C
Explanation
The value of w determines the writes are committed and acknowledged by some minimum number of nodes which in this case is 3.
Q 8 - Which of the following commands create an unique index on author field of the posts collection?
A - db.posts.createIndex({"author":1 }, {"unique": true});
B - db.posts.createIndex({"author": unique });
Answer : A
Explanation
MongoDB cannot create a unique index on the specified index field(s) if the collection already contains data that would violate the unique constraint for the index. The syntax for the same is db.collection.createIndex( { a: 1 }, { unique: true } )
Q 9 - Which of the following is the correct syntax for starting a new mongod process and specifying its replica set name as rs0:
D - First execute this: mongod --replSet. And then execute this: rs.add()
Answer : A
Explanation
Replica set can be set/stated using the –replSet command and specifying the replica name with it.
Q 10 - Consider the following document from the products collection:
{ _id: 1, product_code: "345678", variations: [ { size: "L", price: 1000 }, { size: "M", price: 800 } ] }
What does the following query using $elemMatch return?
db.products.find( { product_code: "345678" }, { variations: { $elemMatch: { size: “L” } } } )
A - Returns the complete document since MongoDB does not support partial array retrieval
B - Returns the document but with only one element in the variations array (corresponding to size L)
C - Returns the complete document but retrieves only the size field from the array
Answer : B
Explanation
The $elemMatch operator limits the contents of an <array> field from the query results to contain only the first element matching the $elemMatch condition.