12 Sep 100 MongoDB MCQ (Multiple Choice Questions) with Answers
1) What type of database is MongoDB?
Show Answer
Answer: c
Explanation
MongoDB is a document-oriented NoSQL database. It stores data as BSON documents inside collections rather than in relational tables.
2) In MongoDB, data is stored in which format?
Show Answer
Answer: b
Explanation
MongoDB stores documents in BSON, a binary-encoded JSON-like format that supports additional data types such as dates and binary data.
3) What is the equivalent of a table in MongoDB?
Show Answer
Answer: b
Explanation
A collection in MongoDB is analogous to a table in a relational database. It groups related documents together.
4) What is the equivalent of a row in MongoDB?
Show Answer
Answer: c
Explanation
A document is the basic unit of data in MongoDB, roughly equivalent to a row in a relational database.
5) What is the primary unique identifier field automatically added to every MongoDB document if not provided?
Show Answer
Answer: b
Explanation
Every document must have a unique
_id field. If you do not provide one, MongoDB automatically generates an ObjectId.6) What is the default data type of the `_id` field?
Show Answer
Answer: c
Explanation
By default, MongoDB uses the ObjectId data type for the
_id field when one is not supplied.7) How many bytes does an ObjectId occupy?
Show Answer
Answer: b
Explanation
An ObjectId is 12 bytes: a 4-byte timestamp, a 5-byte random value, and a 3-byte incrementing counter.
8) Which command starts the MongoDB server process?
Show Answer
Answer: b
Explanation
mongod is the primary daemon process for the MongoDB server. mongos is the routing service for sharded clusters.9) Which process acts as a routing service for MongoDB sharded clusters?
Show Answer
Answer: b
Explanation
mongos acts as a query router, directing client requests to the appropriate shard in a sharded cluster.10) What is the maximum size of a BSON document in MongoDB?
Show Answer
Answer: c
Explanation
MongoDB limits BSON documents to 16 MB. For larger data, GridFS can be used.
11) Which command displays the list of all databases?
Show Answer
Answer: b
Explanation
show dbs lists all databases available on the MongoDB server.12) Which command switches to a specific database named `testDB`?
Show Answer
Answer: b
Explanation
use testDB switches to the specified database. If it does not exist, MongoDB creates it when data is first inserted.13) How do you check which database is currently selected?
Show Answer
Answer: a
Explanation
Typing
db in the MongoDB shell returns the name of the currently selected database.14) Which command is used to drop the current database?
Show Answer
Answer: b
Explanation
db.dropDatabase() deletes the current database, including all its collections and data.15) Which method explicitly creates a collection in MongoDB?
Show Answer
Answer: c
Explanation
db.createCollection() explicitly creates a collection, which is useful when you need to set options such as capped or validator.16) Which command lists all collections in the current database?
Show Answer
Answer: a
Explanation
show collections lists all collections in the currently selected database.17) How do you drop a collection named `users`?
Show Answer
Answer: b
Explanation
db.users.drop() removes the entire users collection, including all documents and indexes.18) What happens when you insert a document into a collection that does not exist?
Show Answer
Answer: c
Explanation
MongoDB creates a collection automatically the first time you insert a document into it.
19) What is a capped collection?
Show Answer
Answer: a
Explanation
A capped collection has a fixed size and automatically overwrites the oldest documents when it reaches capacity, preserving insertion order.
20) Which parameter creates a capped collection using `db.createCollection()`?
Show Answer
Answer: b
Explanation
To create a capped collection, pass
{ capped: true, size: <bytes> }. The size parameter is required.21) Which method inserts a single document into a collection?
Show Answer
Answer: a
Explanation
insertOne() inserts a single document into the specified collection.22) Which method inserts multiple documents at once?
Show Answer
Answer: a
Explanation
insertMany() inserts an array of documents in a single operation.23) If `ordered: false` is passed in `insertMany()`, what happens if one document fails?
Show Answer
Answer: c
Explanation
With
ordered: false, MongoDB continues processing the remaining documents even if some inserts fail.24) What happens if you specify an existing `_id` during an insert operation?
Show Answer
Answer: b
Explanation
The
_id field must be unique. Inserting a document with an existing _id causes a duplicate key error.25) Which method updates a document if found, or inserts it if it doesn’t exist (when `upsert: true`)?
Show Answer
Answer: d
Explanation
Both
updateOne() and replaceOne() support the upsert: true option, which inserts a new document if no match is found.26) Which method is used to query documents from a collection?
Show Answer
Answer: b
Explanation
find() is the primary method for querying documents from a collection.27) How do you return only one matching document from a collection?
Show Answer
Answer: b
Explanation
findOne() returns a single document that matches the query, or null if none match.28) What does `db.collection.find()` return if no query parameters are given?
Show Answer
Answer: c
Explanation
Calling
find() with no filter returns all documents in the collection.29) How do you format output to make it readable in the MongoDB shell?
Show Answer
Answer: b
Explanation
.pretty() formats query results in a more readable, indented JSON-like layout in the shell.30) Which projection syntax includes only the `name` field in query results?
Show Answer
Answer: a
Explanation
The projection
{ name: 1 } includes only the name field (and _id by default).31) By default, is the `_id` field included in projection results?
Show Answer
Answer: b
Explanation
_id is included by default in query results. To exclude it, explicitly set { _id: 0 } in the projection.32) How do you limit the output of a query to 5 records?
Show Answer
Answer: b
Explanation
.limit(5) restricts the result set to at most 5 documents.33) Which method skips the first 10 documents in a result set?
Show Answer
Answer: b
Explanation
.skip(10) skips the first 10 documents in the result set.34) How do you sort results by `age` in ascending order?
Show Answer
Answer: a
Explanation
In MongoDB sort syntax,
1 means ascending order and -1 means descending order.35) How do you sort results by `age` in descending order?
Show Answer
Answer: b
Explanation
sort({ age: -1 }) sorts documents by age in descending order.36) Which operator checks for equality?
Show Answer
Answer: b
Explanation
$eq matches documents where the field value equals the specified value.37) Which operator selects documents where a field value is greater than a specified value?
Show Answer
Answer: a
Explanation
$gt selects documents where the field value is greater than the given value.38) Which operator selects documents where a value is less than or equal to a specified value?
Show Answer
Answer: b
Explanation
$lte matches values that are less than or equal to the specified value.39) Which operator matches any of the values specified in an array?
Show Answer
Answer: a
Explanation
$in matches documents where the field value equals any value in the specified array.40) What is the opposite operator of `$in`?
Show Answer
Answer: b
Explanation
$nin matches documents where the field value is not in the specified array.41) Which logical operator returns documents that satisfy ALL given conditions?
Show Answer
Answer: b
Explanation
$and joins query clauses and requires all clauses to be true.42) Which logical operator matches documents where NEITHER condition is true?
Show Answer
Answer: b
Explanation
$nor matches documents that fail all the specified conditions.43) Which operator checks for the existence of a field in a document?
Show Answer
Answer: b
Explanation
$exists checks whether a field is present in a document.44) Which operator selects documents based on BSON data types?
Show Answer
Answer: a
Explanation
$type filters documents based on the BSON type of a field.45) Which operator allows regex searching in MongoDB queries?
Show Answer
Answer: b
Explanation
$regex allows pattern matching using regular expressions in queries.46) Which operator matches arrays containing ALL specified elements?
Show Answer
Answer: b
Explanation
$all matches arrays that contain every element in the specified array.47) Which operator checks the exact array size in a document?
Show Answer
Answer: c
Explanation
$size matches arrays with the exact number of elements specified.48) Which operator matches documents containing an array field with at least one element matching all criteria?
Show Answer
Answer: b
Explanation
$elemMatch matches documents where at least one array element satisfies all the specified conditions.49) How do you select documents where `age` is between 20 and 30 inclusive?
Show Answer
Answer: b
Explanation
Use
$gte: 20 and $lte: 30 to include both endpoints.50) What operator evaluates a JavaScript expression inside a query?
Show Answer
Answer: b
Explanation
$where passes a JavaScript expression or function to filter documents.51) Which operator sets the value of a field in a document?
Show Answer
Answer: b
Explanation
$set sets the value of a field. If the field does not exist, it is created.52) Which operator deletes a specified field from a document?
Show Answer
Answer: a
Explanation
$unset removes the specified field from a document.53) Which operator increments a field value by a specified number?
Show Answer
Answer: b
Explanation
$inc increments a field by a specified amount.54) How do you decrement a numeric field `points` by 5?
Show Answer
Answer: a
Explanation
Use
$inc with a negative number to decrement a field.55) Which array operator appends a single item to an array field?
Show Answer
Answer: b
Explanation
$push appends a value to an array field.56) Which array operator appends items to an array ONLY if they do not already exist?
Show Answer
Answer: b
Explanation
$addToSet adds a value to an array only if it is not already present, avoiding duplicates.57) Which operator removes the first or last item from an array?
Show Answer
Answer: a
Explanation
$pop removes the first (-1) or last (1) element from an array.58) How do you remove all instances of a specific value from an array?
Show Answer
Answer: b
Explanation
$pull removes all array elements that match a specified value or condition.59) Which method deletes a single matching document from a collection?
Show Answer
Answer: b
Explanation
deleteOne() deletes the first document that matches the filter.60) Which method deletes ALL documents matching a query?
Show Answer
Answer: a
Explanation
deleteMany() deletes all documents that match the specified filter.61) What does `db.collection.deleteMany({})` do?
Show Answer
Answer: b
Explanation
Passing an empty filter
{} to deleteMany() removes all documents but keeps the collection and its indexes.62) Which method replaces an entire single document except the `_id` field?
Show Answer
Answer: b
Explanation
replaceOne() replaces the entire matched document with a new one, preserving the _id.63) What is the primary benefit of creating indexes in MongoDB?
Show Answer
Answer: b
Explanation
Indexes improve the speed of read operations by allowing MongoDB to locate documents without scanning the entire collection.
64) Which method creates an index on a collection?
Show Answer
Answer: b
Explanation
createIndex() builds an index on the specified field or fields.65) How do you create an ascending index on the `email` field?
Show Answer
Answer: a
Explanation
1 specifies ascending order for an index key.66) What is a compound index?
Show Answer
Answer: a
Explanation
A compound index includes more than one field, e.g.
{ lastName: 1, firstName: 1 }.67) Which index type enforces uniqueness for indexed field values?
Show Answer
Answer: b
Explanation
A unique index prevents duplicate values in the indexed field.
68) How do you create a unique index on `username`?
Show Answer
Answer: a
Explanation
Pass the
unique: true option to createIndex() to enforce uniqueness.69) What type of index automatically deletes documents after a specified time period?
Show Answer
Answer: b
Explanation
A TTL index uses a date field to automatically remove documents after a specified number of seconds.
70) Which index type indexes array data by creating entries for each array element?
Show Answer
Answer: a
Explanation
A multikey index is created automatically when you index a field that contains an array.
71) How do you list all indexes on a collection named `orders`?
Show Answer
Answer: d
Explanation
Both
getIndexes() and listIndexes() return information about the indexes on a collection.72) How do you drop an index on field `email`?
Show Answer
Answer: a
Explanation
dropIndex() removes the specified index from the collection.73) What tool/method shows query performance statistics, such as index usage?
Show Answer
Answer: a
Explanation
.explain() provides details about how MongoDB executes a query, including index usage.74) What is a “covered query”?
Show Answer
Answer: a
Explanation
A covered query can be satisfied entirely from the index without reading the actual documents, making it very fast.
75) What default index is created on every collection automatically?
Show Answer
Answer: b
Explanation
MongoDB automatically creates a unique index on the
_id field for every collection.76) Which method is used to process data through an aggregation pipeline?
Show Answer
Answer: b
Explanation
aggregate() runs an aggregation pipeline, which processes documents through multiple stages.77) In aggregation, which stage filters documents (similar to `find()`)?
Show Answer
Answer: b
Explanation
$match filters the document stream to pass only matching documents to the next stage.78) Which aggregation stage groups documents by a specified key?
Show Answer
Answer: b
Explanation
$group groups documents by a specified key and can compute accumulators such as sum, average, etc.79) What field is required inside the `$group` stage to define group keys?
Show Answer
Answer: a
Explanation
In a
$group stage, the _id field defines the group key expression.80) Which aggregation stage reshapes documents by including, excluding, or adding fields?
Show Answer
Answer: b
Explanation
$project includes, excludes, or computes new fields in the output documents.81) Which aggregation stage is used to sort incoming documents?
Show Answer
Answer: b
Explanation
$sort sorts all documents in the pipeline by the specified field(s).82) Which aggregation stage flattens an array field to output a document for each element?
Show Answer
Answer: b
Explanation
$unwind deconstructs an array field, creating one output document per array element.83) Which aggregation stage performs a left outer join to another collection?
Show Answer
Answer: b
Explanation
$lookup performs a left outer join with another collection in the same database.84) Which parameter in `$lookup` specifies the collection in the same database to join?
Show Answer
Answer: a
Explanation
The
from field specifies the collection to join with in a $lookup stage.85) Which aggregation stage outputs results directly into a specified collection?
Show Answer
Answer: a
Explanation
$out writes the aggregation results to a specified collection.86) Which accumulator operator counts the number of documents in a `$group` stage?
Show Answer
Answer: a
Explanation
Using
{ $sum: 1 } in a $group stage counts the number of documents in each group.87) Which operator calculates the average of numeric values in a group?
Show Answer
Answer: b
Explanation
$avg returns the average of numeric values in a group.88) Which aggregation stage restricts the number of documents passed to the next stage?
Show Answer
Answer: b
Explanation
$limit limits the number of documents passed to the next stage in the pipeline.89) What is a Replica Set in MongoDB?
Show Answer
Answer: b
Explanation
A replica set is a group of MongoDB servers that maintain the same data, providing redundancy and failover.
90) How many Primary nodes can a single Replica Set have at any given time?
Show Answer
Answer: a
Explanation
A replica set can have only one primary node at a time; all other nodes are secondaries.
91) What happens if the Primary node fails in a Replica Set?
Show Answer
Answer: b
Explanation
If the primary becomes unavailable, the remaining secondaries hold an election to choose a new primary.
92) What is the role of an Arbiter in a Replica Set?
Show Answer
Answer: b
Explanation
An arbiter participates in elections and votes but does not store data, helping maintain an odd number of voting members.
93) What is Sharding in MongoDB?
Show Answer
Answer: b
Explanation
Sharding distributes data across multiple servers (shards) to support horizontal scaling and large data volumes.
94) What component acts as a query router for a sharded cluster?
Show Answer
Answer: b
Explanation
mongos routes client requests to the appropriate shard in a sharded cluster.95) What is a Shard Key?
Show Answer
Answer: b
Explanation
The shard key is an indexed field or compound field used to distribute documents across shards.
96) What component stores metadata and configuration settings for a sharded cluster?
Show Answer
Answer: b
Explanation
Config servers store metadata and configuration settings for the sharded cluster.
97) Which command enables sharding on a specific database?
Show Answer
Answer: a
Explanation
sh.enableSharding("dbName") enables sharding for the specified database.98) What is the default write concern level in MongoDB?
Show Answer
Answer: b
Explanation
The default write concern is
w: 1, which acknowledges the write after it is applied to the primary node.99) Which authentication mechanism is enabled by default in secured MongoDB setups?
Show Answer
Answer: b
Explanation
SCRAM is the default authentication mechanism in MongoDB, providing salted challenge-response authentication.
100) Which MongoDB utility is used to export data into BSON binary dumps for backup?
Show Answer
Answer: b
Explanation
mongodump creates a binary BSON dump of a MongoDB database, commonly used for backups.
No Comments