- 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 - 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 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 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 - Which option can be used with update command so that a new document gets created if no matching document is found based on the query condition?
A - Specify {upsert : true } as the third parameter of update command
B - upsert command instead of update command
C - {update: true, insert: true} as the third parameter of update command
Answer : A
Explanation
When you specify upsert: true for an update operation and no matching documents are found, MongoDB creates a new document.
Q 5 - Consider the following posts document:
{
_id: 1,
post_text: This is my first post,
author: Tom,
tags: [tutorial,quiz,facebook,learning,fun]
}
Which of the following queries will return the documents but with only the first two tags in the tags array?
A - db.posts.find({author:"Tom"},{tags:{$slice:2}})
B - db.posts.find({author:"Tom"}).limit({tags:2})
C - db.posts.find({author:"Tom"}).limit($slice:{tags:2})
D - Both a and c are valid. $slice works both with projection and limit.
Answer : A
Explanation
The $slice operator controls the number of items of an array that a query returns.
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 - 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 - 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 tags in a replica set configuration specify the operations to be read from the node with the least network latency?
Answer : C
Explanation
Operations read from member of the replica set with the least network latency, irrespective of the members type.
Q 10 - The ________ operator limits the contents of an array field from the query results to contain only the first element matching the query condition.
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.