Exit
Learn Mode
Play Instead

MongoDB / Learn

Master 100 concepts with detailed explanations.

1basic

What type of database is MongoDB?

Document-oriented NoSQL database that stores data as flexible BSON documents in collections.

MongoDB is a document-oriented NoSQL database. It stores data as BSON (Binary JSON) documents in collections. Unlike relational databases, it does not require a fixed schema, making it flexible for evolving data structures.

2basic

What is the purpose of an index in MongoDB?

To improve query performance by allowing efficient lookups without full collection scans.

Indexes store a small portion of the collection's data in a sorted structure. This allows MongoDB to find matching documents without scanning every document in the collection (COLLSCAN). Without indexes, queries on large collections are very slow.

3medium

What does the aggregation pipeline do in MongoDB?

Processes documents through a sequence of stages that transform, filter, group, or compute data.

The aggregation pipeline passes documents through stages like $match (filter), $group (aggregate), $project (reshape), $sort, $limit, and $lookup (join). Each stage transforms the data for the next, enabling complex analytics queries.

4medium

What is a replica set in MongoDB?

A group of MongoDB instances that maintain the same data set for high availability and automatic failover.

A replica set is a group of mongod instances that replicate data between each other. One node is primary (accepts writes); others are secondaries (replicate data). If the primary fails, an election promotes a secondary, providing automatic failover.

5medium

What does the $lookup stage do in an aggregation pipeline?

Performs a left outer join with another collection, embedding matched documents as an array field.

$lookup performs a left outer join between the current collection and a foreign collection, matching documents by a local field to a foreign field. The joined documents are added as an array field to each input document.

6medium

What is sharding in MongoDB?

Distributing data across multiple machines (shards) for horizontal scalability beyond a single server.

Sharding horizontally partitions data across multiple servers (shards) using a shard key. Each shard holds a subset of the data. A mongos router directs queries to the appropriate shard(s), enabling MongoDB to scale beyond the capacity of a single machine.

7advanced

Which MongoDB operator would you use to update a field only if the document does not already have it?

$setOnInsert, which only applies its values when the upsert operation results in a new insert.

$setOnInsert only applies its update when the operation results in an insert (upsert). If the document already exists and is being updated, $setOnInsert has no effect. It is commonly used with upsert: true to initialise fields only on creation.

8basic

What is the default value of _id in MongoDB documents?

An ObjectId, a 12-byte BSON type encoding a timestamp, random value, and incrementing counter.

MongoDB automatically generates an ObjectId for _id if not provided. An ObjectId is 12 bytes: 4-byte Unix timestamp, 5-byte random value (machine+process), and 3-byte incrementing counter. This makes it globally unique and roughly sortable by creation time.

9advanced

What does db.collection.explain('executionStats') tell you?

The query execution plan, including whether an index was used and how many documents were examined.

explain('executionStats') returns the winning query plan and detailed stats like nReturned, totalDocsExamined, and totalKeysExamined. A high totalDocsExamined relative to nReturned means the query is doing a collection scan and likely needs an index.

10advanced

What is a sparse index in MongoDB?

An index that only contains entries for documents that have the indexed field, skipping documents where the field is absent.

A sparse index only includes documents that contain the indexed field. Documents missing the field are excluded from the index entirely. This is useful for optional fields where most documents don't have the field, keeping the index small.

11basic

Which command inserts a single document into a MongoDB collection?

db.users.insertOne({ name: "Alice", age: 30, role: "admin" })
insertOne() inserts the document and returns an object containing the acknowledged flag and the inserted _id.

insertOne() inserts exactly one document and returns an InsertOneResult with acknowledged: true and the insertedId. The older insert() method is deprecated. insertMany() accepts an array and is used for multiple documents.

12basic

What does the following query return?

db.orders.find({ status: "shipped", amount: { $gt: 100 } })
All orders where both status equals 'shipped' AND amount is greater than 100, using implicit AND logic.

When multiple fields are specified in a find() filter object, MongoDB applies implicit AND logic: all conditions must be true. Here, only documents where status is 'shipped' AND amount is greater than 100 are returned.

13medium

Which operator checks if a field exists in a document, regardless of its value?

$exists filters documents based on whether a field is present or absent in the document.

$exists: true returns documents that contain the specified field, even if the field's value is null. $exists: false returns documents where the field is absent. Note that a field with value null IS present; $exists: true would match it.

14basic

What does the $in operator do?

db.products.find({ category: { $in: ["electronics", "books", "toys"] } })
$in matches documents where the field value equals any one of the values in the provided array.

$in matches documents where the field value equals any of the values in the given array. It is equivalent to an OR condition across those values. For array fields, $in matches if any element in the field array equals any value in the $in array.

15basic

What does the $set operator do in an update operation?

db.users.updateOne({ _id: userId }, { $set: { email: "new@example.com", updatedAt: new Date() } })
$set sets or updates specific fields in the document without affecting any other fields.

$set updates or creates the specified fields without modifying any other fields in the document. If a field doesn't exist, $set creates it. To replace an entire document you would use replaceOne() or omit update operators entirely.

16basic

What does $unset do in MongoDB?

$unset removes one or more specified fields from the matched documents entirely.

$unset removes the specified field from the document. The field key and its value are both deleted. You can pass any string as the value (conventionally an empty string or 1) since only the key matters to $unset.

17basic

Which update operator increments a numeric field by a given amount?

db.counters.updateOne({ name: "pageViews" }, { $inc: { count: 1 } })
$inc increments (or decrements, with a negative value) the field by the specified amount atomically.

$inc atomically increments a numeric field by the given value. A negative value decrements the field. This is safe for concurrent updates because MongoDB applies the increment atomically at the document level.

18basic

What does $push do to an array field?

$push appends a value to the end of an existing array field in the matched document.

$push appends one element to an existing array. If the field does not exist, $push creates it as a single-element array. To prevent duplicates, use $addToSet instead. $push can also be used with $each to append multiple values at once.

19medium

What is the difference between $pull and $pop in MongoDB?

$pull removes elements by value or condition, while $pop removes the first or last element by position.

$pull removes all elements from an array that match a specified value or condition. $pop removes the first element ($pop: -1) or the last element ($pop: 1) by position. They serve different use cases for array manipulation.

20medium

What does $addToSet do differently from $push?

$addToSet adds a value to an array only if the value does not already exist, preventing duplicates.

$addToSet treats the array as a set and only adds the element if it is not already present. $push always appends, even if the value already exists. $addToSet is ideal for maintaining a list of unique tags, roles, or identifiers.

21medium

What does the upsert option do in updateOne()?

db.users.updateOne({ email: "a@b.com" }, { $set: { name: "Alice" } }, { upsert: true })
upsert: true causes MongoDB to update the document if it exists, or insert a new one if it does not.

With upsert: true, if no document matches the filter, MongoDB inserts a new document combining the filter fields and the update fields. If a document matches, it is updated normally. The result object includes an upsertedId when an insert occurs.

22medium

What is the purpose of the $elemMatch query operator?

db.students.find({ scores: { $elemMatch: { subject: "math", grade: { $gte: 90 } } } })
$elemMatch matches documents where at least one array element satisfies all specified conditions together.

$elemMatch ensures that a single array element satisfies all the specified criteria simultaneously. Without $elemMatch, each condition could be satisfied by different elements. It is essential when matching multi-field subdocuments inside arrays.

23basic

Which aggregation stage would you use to filter documents early in a pipeline?

$match is the correct stage to filter documents, and placing it early improves performance significantly.

$match filters documents using the same query syntax as find(). Placing $match early in the pipeline reduces the number of documents passed to subsequent stages, improving performance and allowing MongoDB to use indexes on the filter fields.

24medium

What does the $group stage require in an aggregation pipeline?

db.orders.aggregate([ { $group: { _id: "$customerId", total: { $sum: "$amount" } } } ])
$group requires an _id field that defines the grouping key; it can be null to aggregate all documents.

$group requires an _id expression that specifies the grouping key. Setting _id to null groups all documents into one. Accumulator expressions like $sum, $avg, $push, and $first compute values across all documents in each group.

25medium

What does the $unwind stage do in an aggregation pipeline?

$unwind deconstructs an array field, outputting one document per array element with the field replaced by a single value.

$unwind outputs one document for each element of the specified array field. The array field is replaced with the individual element value. This is useful before $group or $lookup when you need to operate on individual array elements.

26medium

How do you create a compound index on two fields in MongoDB?

db.orders.createIndex({ customerId: 1, createdAt: -1 })
You pass an object with multiple field-direction pairs to createIndex(), creating a single compound index.

A compound index is created by passing an object with multiple field-direction pairs to createIndex(). MongoDB traverses the index using the leftmost prefix rule: queries must use fields from left to right for the index to be effective.

27advanced

What is the compound index prefix rule in MongoDB?

A query can use a compound index only if it includes the leftmost field(s) of the index in order.

A compound index on {a, b, c} supports queries on {a}, {a, b}, and {a, b, c}. Queries on {b} or {c} alone cannot use the index because they skip the leftmost prefix field. Index field order matters significantly for which queries benefit.

28medium

What is a TTL index and what is it used for?

A TTL index automatically deletes documents after a specified number of seconds past a date field's value.

A TTL (Time To Live) index is a special single-field index on a Date field. MongoDB's background thread removes documents when the date field value plus the expireAfterSeconds setting is in the past. It is commonly used for sessions, caches, and logs.

29advanced

What is a covered query in MongoDB?

A covered query is satisfied entirely by the index without examining any underlying documents.

A covered query is one where all queried fields and all projected fields are part of the same index. MongoDB can return results from the index alone without fetching actual documents, making it extremely fast. The explain() output shows IXSCAN with no FETCH stage.

30medium

What is the difference between COLLSCAN and IXSCAN in explain() output?

COLLSCAN means MongoDB scanned all documents in the collection; IXSCAN means an index was used for the query.

COLLSCAN (collection scan) means MongoDB examined every document in the collection to find matches, which is slow at scale. IXSCAN (index scan) means MongoDB used an index to efficiently locate matching documents. IXSCAN is almost always preferable for large collections.

31advanced

What write concern does { w: 'majority' } specify?

w: 'majority' waits until the write is acknowledged by a majority of voting replica set members before returning.

w: 'majority' tells MongoDB to wait until the write is acknowledged by a majority of voting members in the replica set. This provides strong durability guarantees because the write survives even if the primary fails and a new primary is elected.

32advanced

What does the j: true write concern option do?

j: true requests acknowledgement only after the write is committed to the on-disk journal, ensuring durability.

j: true means MongoDB will not acknowledge the write until it has been written to the on-disk journal. This protects against data loss from a process crash, because the journal survives restarts. Combined with w: 'majority', it provides the strongest durability.

33advanced

What read preference setting routes reads to the nearest replica set member?

nearest routes reads to the member with the lowest measured network latency, primary or secondary.

The 'nearest' read preference selects the member with the lowest network round-trip time, regardless of whether it is primary or secondary. It is useful for globally distributed applications where latency matters more than reading the absolute latest data.

34advanced

What is read concern 'majority' in MongoDB?

Read concern 'majority' returns only data that has been acknowledged by a majority of replica set members, preventing stale reads.

Read concern 'majority' returns data that has been acknowledged by a majority of voting replica set members. This prevents reading data that could be rolled back if the primary fails. It provides a consistent view of data that is guaranteed to be durable.

35advanced

Which read concern guarantees that you read your own most recently committed write?

Read concern 'linearizable' guarantees you read the most recently committed data, reflecting all prior writes.

Read concern 'linearizable' guarantees that a read reflects all prior writes that were majority-committed before the read began. It provides the strongest guarantee but has higher latency. It can only be used with primary read preference.

36advanced

How do you start a multi-document transaction in MongoDB?

const session = client.startSession(); session.startTransaction(); try { await db.collection("accounts").updateOne({ _id: fromId }, { $inc: { balance: -100 } }, { session }); await db.collection("accounts").updateOne({ _id: toId }, { $inc: { balance: 100 } }, { session }); await session.commitTransaction(); } catch (err) { await session.abortTransaction(); }
You call client.startSession(), then session.startTransaction(), and pass the session to every operation involved.

Multi-document transactions require a ClientSession. You start a session, call startTransaction(), pass the session object to every operation in the transaction, then commit or abort. All operations in the session are atomic across documents and collections.

37advanced

What minimum MongoDB deployment is required to use multi-document transactions?

Multi-document transactions require a replica set; standalone mongod instances do not support them.

Multi-document ACID transactions require a replica set (introduced in MongoDB 4.0). Standalone instances do not support them. Sharded cluster transactions were added in MongoDB 4.2, building on the replica set transaction foundation.

38medium

What is the purpose of GridFS in MongoDB?

GridFS is a specification for storing and retrieving files larger than the 16 MB BSON document limit.

GridFS splits large files into chunks (default 255 KB each) stored in a 'chunks' collection, with metadata in a 'files' collection. It works around the 16 MB BSON document limit and supports streaming of large files like videos and images.

39medium

What is a capped collection in MongoDB?

A capped collection has a fixed size in bytes; when full, it overwrites the oldest documents automatically.

Capped collections have a fixed maximum size in bytes (and optionally a document count limit). When the collection is full, new inserts overwrite the oldest documents in insertion order. They support tailable cursors, making them useful for log-like workloads.

40advanced

What is a tailable cursor in MongoDB?

A tailable cursor remains open after returning the last document, waiting for new documents to be inserted.

A tailable cursor works only on capped collections. After exhausting all current documents, it remains open and returns new documents as they are inserted. This is similar to 'tail -f' in Unix and is useful for real-time log or event stream consumption.

41advanced

What is a change stream in MongoDB?

A change stream allows applications to subscribe to real-time notifications of data changes in a collection, database, or cluster.

Change streams use the oplog to notify applications of inserts, updates, deletes, and schema changes in real time. They require a replica set or sharded cluster. Applications call collection.watch() to open a change stream and iterate over change events.

42advanced

What is the oplog in a MongoDB replica set?

The oplog is a special capped collection that records all write operations applied to the primary so secondaries can replicate them.

The oplog (operations log) is a special capped collection (local.oplog.rs) on each replica set member. It records every write operation applied to the primary in an idempotent form. Secondaries tail the primary's oplog to replicate all writes.

43medium

What is the role of an arbiter in a MongoDB replica set?

An arbiter participates in elections to break ties but holds no data and cannot become primary.

An arbiter holds no data and cannot become a primary. Its only role is to vote in elections, providing an odd number of votes to break ties. Arbiters are used to achieve election quorum with fewer hardware resources than a full data-bearing secondary.

44medium

What is the shard key in a MongoDB sharded cluster?

The shard key is the field (or fields) MongoDB uses to distribute documents across shards in a cluster.

The shard key determines how MongoDB distributes documents across shards. Each document's shard key value maps to a chunk range, and each chunk lives on one shard. Choosing a high-cardinality, evenly distributed shard key is critical for balanced clusters.

45advanced

What is the difference between ranged sharding and hashed sharding?

Ranged sharding routes documents by shard key value ranges; hashed sharding routes by a hash of the key, distributing more evenly.

Ranged sharding assigns contiguous value ranges of the shard key to each shard, which can cause hotspots with monotonically increasing keys. Hashed sharding applies a hash function first, distributing documents more evenly but losing range query locality.

46advanced

What is a scatter-gather query in a sharded cluster?

A scatter-gather query is one that runs in parallel on all shards because it lacks the shard key in its filter.

When a query does not include the shard key, mongos must broadcast it to all shards (scatter) and merge the results (gather). This is far less efficient than a targeted query that includes the shard key and routes to exactly one shard.

47basic

What does the $project stage do in an aggregation pipeline?

db.users.aggregate([ { $project: { name: 1, email: 1, _id: 0 } } ])
$project reshapes documents by including or excluding fields, adding computed fields, or renaming fields.

$project controls which fields appear in the output documents. Setting a field to 1 includes it; setting it to 0 excludes it. You can also add computed fields using expressions. _id is included by default and must be explicitly set to 0 to exclude it.

48basic

How do you exclude the _id field from a find() projection?

db.users.find({ role: "admin" }, { name: 1, email: 1, _id: 0 })
You set _id: 0 in the projection document; _id is the only field that can be excluded while including others.

_id is included by default in every query result. To exclude it, explicitly set _id: 0 in the projection. This is the only case where you can mix inclusion (1) and exclusion (0) in the same projection document.

49medium

What does the $mul update operator do?

$mul multiplies the value of a numeric field by the specified factor and stores the result.

$mul multiplies the current value of a field by the given number. If the field does not exist, $mul sets it to 0 (for integer/decimal types). It is atomic and useful for applying percentage increases or unit conversions.

50medium

What does the $min update operator do?

$min updates a field to the given value only if the given value is less than the current field value.

$min updates a field to the new value only if that value is lower than the existing field value. If the field does not exist, $min sets it to the specified value. Its counterpart $max updates only when the new value is greater than the current value.

51basic

What is the difference between deleteOne() and deleteMany() in MongoDB?

deleteOne() removes the first document matching the filter; deleteMany() removes all documents matching the filter.

deleteOne() removes exactly one document matching the filter (the first one found by the query plan). deleteMany() removes all documents that match the filter. Both return a result object with a deletedCount property indicating how many documents were removed.

52medium

What is the difference between replaceOne() and updateOne() in MongoDB?

replaceOne() replaces the entire document except for _id; updateOne() modifies specific fields using update operators.

replaceOne() replaces all fields of the matched document with the replacement document (keeping the same _id). updateOne() uses update operators ($set, $inc, etc.) to modify specific fields. Using updateOne() without operators would throw an error.

53medium

What does the $rename update operator do?

$rename renames a field in matched documents, moving its value to the new field name atomically.

$rename renames a field by removing the old field and creating a new field with the new name and the same value. It is equivalent to performing $set on the new name and $unset on the old name atomically in a single operation.

54medium

What does the $currentDate update operator do?

$currentDate sets a field to the current date or timestamp at the time the update is executed on the server.

$currentDate sets a field to the server's current date as a BSON Date type (by default) or a Timestamp. It is useful for automatically stamping updatedAt fields without relying on the client to provide the correct time.

55medium

What is the $regex operator used for in MongoDB queries?

db.users.find({ name: { $regex: "^Ali", $options: "i" } })
$regex matches documents where a string field satisfies a given regular expression pattern.

$regex matches string fields against a regular expression. The $options modifier (e.g., 'i' for case-insensitive, 'm' for multiline) controls matching behaviour. For performance, use an anchored prefix regex (^pattern) which can utilise a string index.

56medium

What does the $all operator do when querying array fields?

db.products.find({ tags: { $all: ["sale", "electronics"] } })
$all matches documents where the array field contains all of the specified values, in any order.

$all matches documents where the array field contains every value in the provided list, regardless of order or additional elements. A document with tags ['sale', 'electronics', 'new'] would match. $all is equivalent to multiple $elemMatch conditions for simple scalar values.

57medium

What does the $size array operator do?

db.posts.find({ comments: { $size: 3 } })
$size matches documents where the specified array field has exactly the given number of elements.

$size matches documents where the array field contains exactly the specified number of elements. It does not support ranges (you cannot use $gt with $size); for range-based array length queries you need to store the count as a separate field and index it.

58medium

What is embedding versus referencing in MongoDB schema design?

Embedding stores related data inside the same document; referencing stores related data in a separate collection linked by an ID.

Embedding places related data inside the same document (denormalised), which is great for data accessed together and has a one-to-few relationship. Referencing stores a reference ID and requires a separate query or $lookup, suitable for large or frequently updated related data.

59medium

When should you prefer referencing over embedding in MongoDB schema design?

Prefer referencing when the related data is large, frequently updated, or shared across many parent documents.

Referencing is better when the related data grows without bound, is updated independently of the parent, exceeds the 16 MB document limit when embedded, or needs to be shared across many parent documents. Embedding works best for data always accessed together.

60basic

What is the maximum document size in MongoDB?

The maximum document size is 16 MB in BSON format, including all nested fields and embedded documents.

BSON documents in MongoDB are limited to 16 MB. This limit prevents individual documents from consuming excessive RAM and network bandwidth. For files or data exceeding this limit, use GridFS which splits content into 255 KB chunks stored in a separate collection.

61basic

What does BSON stand for and how does it differ from JSON?

BSON stands for Binary JSON; it is a binary-encoded serialisation of JSON-like documents supporting additional types.

BSON (Binary JSON) is a binary-encoded format used internally by MongoDB. It extends JSON with additional types like Date, ObjectId, Binary, Decimal128, and Int64. BSON is more efficient to parse and supports richer data types than plain JSON.

62advanced

What is the Decimal128 BSON type used for?

Decimal128 stores high-precision decimal floating-point numbers, suitable for financial and monetary values.

Decimal128 is a 128-bit IEEE 754-2008 decimal floating-point type with 34 significant decimal digits and a range of 10^-6143 to 10^6144. It avoids binary floating-point rounding errors, making it the correct choice for currency amounts and precise scientific data.

63advanced

What does MongoDB store when a JavaScript application sets a field to undefined?

MongoDB drivers strip fields set to undefined before sending the document; the field is simply not stored.

MongoDB drivers treat undefined values the same as missing fields and omit them when serialising to BSON. The BSON Undefined type exists for legacy reasons but is deprecated. To store an explicit 'no value', use null, which maps to the BSON Null type.

64medium

What is the purpose of the $addFields aggregation stage?

db.orders.aggregate([ { $addFields: { totalWithTax: { $multiply: ["$total", 1.1] } } } ])
$addFields adds new fields or overwrites existing fields while keeping all other document fields intact.

$addFields is like $project but preserves all existing fields. It adds new computed fields or overwrites existing ones. It is useful when you want to enrich documents with derived values without having to explicitly include every existing field in a $project stage.

65advanced

What does the $replaceRoot aggregation stage do?

$replaceRoot promotes a nested subdocument to become the top-level document, replacing all parent fields.

$replaceRoot makes a specified subdocument the new root of the output document. The original root is discarded. It is commonly used after $unwind or $lookup to lift embedded documents to the top level for further processing.

66advanced

What does the $facet aggregation stage enable?

$facet runs multiple independent sub-pipelines on the same input documents in a single stage, returning all results together.

$facet processes multiple independent aggregation pipelines within the same stage, each receiving the same input documents. The results of all sub-pipelines are returned in a single document. It is ideal for generating multiple aggregations (counts, ranges, categories) in one query.

67advanced

What is the purpose of the $bucket aggregation stage?

$bucket categorises documents into ranges (buckets) based on a specified expression and boundary values.

$bucket groups documents into user-defined ranges based on a field value. You specify boundaries like [0, 10, 50, 100] and each document is placed in the range its value falls into. $bucketAuto automatically determines equal-count boundaries.

68medium

What does the $out aggregation stage do?

$out writes the aggregation pipeline results to a specified collection, replacing it if it exists.

$out writes all output documents to a named collection, replacing the collection entirely if it exists. Unlike $merge, $out is all-or-nothing: if the pipeline fails, the existing collection is left unchanged. $out must be the last stage in the pipeline.

69advanced

How does $merge differ from $out in the aggregation pipeline?

$merge can insert, update, replace, or merge pipeline results into an existing collection without replacing it entirely.

$merge (introduced in MongoDB 4.2) writes pipeline results to an existing collection with fine-grained control: you can insert new documents, update existing ones, replace them, or run custom merge pipelines. $out replaces the entire target collection atomically.

70medium

What is a text index in MongoDB and how do you query it?

db.articles.createIndex({ title: "text", body: "text" }); db.articles.find({ $text: { $search: "mongodb aggregation" } })
A text index tokenises string fields for full-text search; you query it using the $text operator with $search.

A text index tokenises string fields, removes stop words, and stems terms. You query using $text: { $search: 'terms' }. A collection can have only one text index but it can cover multiple fields. Use $meta: 'textScore' to sort by relevance.

71advanced

What is a multikey index in MongoDB?

A multikey index is automatically created when you index an array field; MongoDB indexes each array element separately.

MongoDB automatically creates a multikey index when the indexed field contains an array. Each array element gets its own index entry. This allows efficient queries on array contents but means a compound index cannot have more than one array field.

72advanced

What is a partial index in MongoDB?

A partial index only indexes documents that satisfy a specified filter expression, reducing index size.

A partial index includes only documents that match a filter expression (e.g., { status: { $eq: 'active' } }). It is smaller and faster than a full index because it excludes irrelevant documents. Queries must include the filter condition to use the index.

73advanced

What is a geospatial index in MongoDB and when would you use it?

A geospatial index enables efficient queries on location data such as finding points near a coordinate or within a polygon.

MongoDB supports 2dsphere indexes for GeoJSON data and 2d indexes for legacy coordinate pairs. They enable operators like $near, $geoWithin, and $geoIntersects for proximity searches, polygon containment, and geographic shape queries.

74medium

What is the difference between mongodump/mongorestore and mongoexport/mongoimport?

mongodump creates BSON binary backups preserving all types; mongoexport creates JSON/CSV text files for data portability.

mongodump outputs BSON binary files that preserve all BSON types and are best for full backups. mongoexport produces JSON or CSV files, which are human-readable and portable but may lose type fidelity (e.g., Dates become strings). Use mongodump for backups and mongorestore to restore.

75medium

What is the standard MongoDB connection string (URI) format?

mongodb://username:password@host1:27017,host2:27017/dbname?replicaSet=myRS&authSource=admin
The standard URI starts with mongodb:// followed by optional credentials, host(s), port(s), database, and options as query parameters.

The standard MongoDB URI format is mongodb://[user:password@]host[:port][/database][?options]. The mongodb+srv:// scheme is a DNS SRV variant that discovers replica set members automatically. Options like replicaSet, authSource, tls, and readPreference are passed as query parameters.

76medium

What does the bulkWrite() method do and what is the difference between ordered and unordered bulk operations?

bulkWrite() executes multiple write operations in one call; ordered stops on first error, unordered continues and reports all errors.

bulkWrite() accepts an array of insertOne, updateOne, updateMany, deleteOne, deleteMany, and replaceOne operations. With ordered: true (default), it stops on the first error. With ordered: false, it continues all operations and returns a combined error list.

77medium

What is a Mongoose schema and how does it relate to a Mongoose model?

A Mongoose schema defines the structure and validation rules for documents; a model is the compiled interface for querying a collection.

A Mongoose Schema defines field names, types, default values, validators, and middleware hooks. A Model is created from a Schema using mongoose.model('Name', schema) and provides a class with static methods (find, create, updateOne) and instance methods for interacting with the MongoDB collection.

78medium

What is a Mongoose virtual field?

A virtual field is a computed property that is not stored in MongoDB but is derived from other document fields.

Mongoose virtuals are schema properties that are not persisted to MongoDB. They are computed on the fly from other fields (e.g., a fullName getter that concatenates firstName and lastName). Virtuals can have both get and set functions and can be populated like references.

79medium

What does Mongoose populate() do?

populate() automatically replaces referenced ObjectId fields with the full documents from the referenced collection.

Mongoose populate() performs a separate query to fetch referenced documents and replaces the ObjectId field with the full document. It is similar to $lookup but works at the ODM level. populate() can be nested and configured to select only specific fields.

80medium

What are Mongoose middleware hooks (pre and post)?

Mongoose hooks are functions that run before (pre) or after (post) specific operations like save, find, and remove.

Mongoose middleware (hooks) are functions executed at specific points in a document's lifecycle. Pre hooks run before the operation (e.g., hashing a password before save). Post hooks run after the operation completes. They are attached with schema.pre('save', fn) and schema.post('find', fn).

81medium

How does MongoDB handle a $lookup when no matching document is found in the foreign collection?

$lookup performs a left outer join: documents with no match in the foreign collection receive an empty array.

$lookup is a left outer join. When no documents in the 'from' collection match the join condition, the 'as' field is set to an empty array []. The source document is still included in the output. You can filter out non-matches with a subsequent $match on the array being non-empty.

82basic

What does the $count aggregation stage do?

db.orders.aggregate([ { $match: { status: "completed" } }, { $count: "completedOrders" } ])
$count outputs a single document with a field containing the total number of documents that reached that stage.

$count outputs a single document with one field (named by the string argument) whose value is the total count of documents at that stage. It is equivalent to $group with $sum:1 but more concise. It is always placed after filtering stages.

83medium

What does the $cond aggregation expression do?

{ $project: { category: { $cond: { if: { $gte: ["$price", 100] }, then: "premium", else: "standard" } } } }
$cond is a ternary expression that evaluates a condition and returns one of two values based on the result.

$cond is a ternary operator: if the 'if' expression is true, it returns the 'then' value; otherwise it returns the 'else' value. It can be used in $project, $addFields, $group, and other expression contexts to create conditional computed fields.

84medium

What is the $first accumulator in a $group stage?

$first returns the value of an expression from the first document that arrives in each group.

$first returns the value of a field from the first document processed in each group. Without a preceding $sort, the 'first' document is indeterminate. With $sort: { date: 1 } before $group, $first gives the earliest value per group, which is a common pattern.

85advanced

What is Atlas Search and how does it differ from MongoDB's built-in text indexes?

Atlas Search is powered by Apache Lucene, offering relevance ranking, faceting, fuzzy matching, and autocomplete beyond what text indexes provide.

Atlas Search is a full-text search engine built on Apache Lucene, available on MongoDB Atlas. It provides relevance scoring, fuzzy matching, autocomplete, synonyms, facets, and complex query operators via the $search aggregation stage. MongoDB's built-in text index is simpler and less feature-rich.

86medium

What does the $type operator do in a MongoDB query?

db.data.find({ value: { $type: "string" } })
$type matches documents where the specified field holds a value of the given BSON data type.

$type matches documents where the given field contains a value of the specified BSON type. You can use the type name (e.g., 'string', 'int', 'date') or the numeric BSON type code. It is useful for finding documents with mixed types stored in the same field.

87medium

What does the findOneAndUpdate() method return by default?

findOneAndUpdate() returns the original document before the update was applied by default.

findOneAndUpdate() returns the original document (before the update) by default. To return the modified document, set the returnDocument option to 'after' (driver API) or returnNewDocument: true (shell). This method atomically finds, updates, and returns a document in one operation.

88medium

What is the $ne operator used for?

db.users.find({ status: { $ne: "inactive" } })
$ne matches documents where the specified field does not equal the given value, including documents where the field is absent.

$ne matches documents where the field value does not equal the specified value. It also matches documents where the field does not exist. This means $ne: null matches documents where the field has a non-null value. For strict not-equal-and-must-exist, combine with $exists: true.

89medium

What is the $nin operator in MongoDB?

$nin matches documents where the field value is not in the provided array and documents where the field does not exist.

$nin (not in) matches documents where the field value does not match any value in the provided array. Like $ne, it also matches documents where the field is absent. It is the complement of $in and is useful for blacklist-style exclusion queries.

90basic

What is the purpose of the $skip aggregation stage?

$skip skips the first N documents in the pipeline and passes the rest to the next stage.

$skip discards the first N documents from the pipeline and forwards the remaining documents to the next stage. Combined with $sort and $limit, it implements cursor-based pagination. For large datasets, using cursor-based pagination with _id comparisons is more efficient than $skip.

91medium

What happens when you call insertMany() with an array of documents and one document fails validation?

With ordered: true (default), insertMany() stops at the first failure and documents after the failed one are not inserted.

With ordered: true (default), insertMany() processes documents sequentially and stops at the first error. Documents inserted before the failure remain committed; documents after are not attempted. With ordered: false, all documents are attempted and all errors are collected.

92medium

What is the $dateToString aggregation expression used for?

{ $project: { formattedDate: { $dateToString: { format: "%Y-%m-%d", date: "$createdAt" } } } }
$dateToString converts a BSON Date value into a formatted string using a strftime-style format pattern.

$dateToString converts a BSON Date value to a formatted string. The format uses strftime-style specifiers like %Y (year), %m (month), %d (day), %H (hour), %M (minute), %S (second). It is commonly used in $project or $group to format dates for output or grouping.

93basic

What is the unique index option and what does it enforce?

db.users.createIndex({ email: 1 }, { unique: true })
A unique index ensures that no two documents in the collection have the same value for the indexed field.

A unique index rejects any insert or update that would result in a duplicate value for the indexed field. Multiple documents with null values are allowed unless combined with a sparse index. Compound unique indexes enforce uniqueness across the combination of indexed fields.

94medium

What does the $concat aggregation expression do?

{ $project: { fullName: { $concat: ["$firstName", " ", "$lastName"] } } }
$concat concatenates two or more string expressions into a single string value.

$concat takes an array of string expressions and joins them into one string with no separator. To add separators, include them as literal string elements in the array. If any input is null or missing, the result is null unless you handle it with $ifNull.

95advanced

Which election priority rule determines which node becomes primary in a replica set election?

The node with the highest configured priority wins, and ties are broken by the most up-to-date oplog position.

In a replica set election, a node must receive votes from a majority of voting members. Nodes with higher priority values are preferred as candidates. Among equal-priority nodes, the one with the most recent oplog entry (closest to the former primary's state) wins the election.

96medium

What is the default read preference for MongoDB driver connections?

The default read preference is 'primary', directing all read operations to the primary replica set member.

The default read preference is 'primary', meaning all reads go to the primary member. This ensures you always read the most recent data. Using 'secondary' can improve throughput but may return stale data if the secondary lags behind the primary.

97basic

What does the $avg accumulator compute in a $group stage?

db.sales.aggregate([ { $group: { _id: "$region", avgRevenue: { $avg: "$revenue" } } } ])
$avg computes the arithmetic mean of numeric values across all documents in each group.

$avg calculates the arithmetic mean of all numeric values of the specified expression across documents in the same group. Non-numeric values and missing fields are ignored. It is one of the standard accumulators available in $group, $project, and $addFields.

98medium

What is the purpose of the $addToSet accumulator in a $group stage?

$addToSet accumulates an array of unique values from the grouped documents, discarding duplicates.

When used as a $group accumulator, $addToSet collects field values from all documents in each group into an array, including each unique value only once. The output array order is not guaranteed. Use $push instead if you want all values including duplicates.

99medium

What is the mongos process in a MongoDB sharded cluster?

mongos is the query router that accepts client connections and routes operations to the appropriate shard(s).

mongos is the routing layer in a sharded cluster. Applications connect to mongos instead of individual shards. mongos reads chunk distribution metadata from the config servers and routes each operation to the correct shard(s). Multiple mongos instances can run for load balancing and high availability.

100advanced

What is a covered query and what conditions must be met for MongoDB to execute one?

A covered query is satisfied entirely from the index: all queried fields and all projected fields must be part of the same index, and _id must be excluded if it is not in the index.

For a query to be covered: every field in the query filter must be in the index, every field in the projection must be in the index, and _id must be excluded from the projection (unless _id is in the index). When all conditions are met, MongoDB reads only index data, with no document fetches, resulting in maximum performance.