MongoDB Answers
0:000:00
MongoDB Answers
Basic MongoDB Answers
# | Question | Answer | Examples |
---|---|---|---|
1 | What is MongoDB? | A NoSQL document database | Stores data in JSON-like documents |
2 | What is a Document? | A unit of data in MongoDB, containing key-value pairs (like JSON) | { "_id": ObjectId(""), "name": "Alice", "age": 30 } |
3 | What is a Collection? | A group of related documents (similar to a table in SQL) | A users collection containing user documents |
4 | What is a Database? | A container for collections | A my_db database containing users and products collections |
5 | What is BSON? | Binary JSON; the serialization format MongoDB uses for documents | Supports more data types than standard JSON (e.g., Date, ObjectId) |
6 | How do you connect to MongoDB? | Using the mongosh shell or by connecting via an application driver | mongosh "mongodb://localhost:27017" |
7 | How do you create a database? | Using the use command (it's created when the first data is inserted) | use my_new_db; |
8 | How do you create a collection? | Using the db.createCollection() command | db.createCollection("products"); |
9 | How do you insert a document? | Using the insertOne() method | db.users.insertOne({ name: "Bob", age: 25 }); |
10 | How do you insert multiple documents? | Using the insertMany() method | db.users.insertMany([{ name: "Charlie" }, { name: "David" }]); |
11 | How do you find documents? | Using the find() method | db.users.find({ age: 30 }); |
12 | How do you find a single document? | Using the findOne() method | db.users.findOne({ name: "Alice" }); |
13 | How do you update documents? | Using the updateOne() or updateMany() methods | db.users.updateOne({ name: "Bob" }, { $set: { age: 26 } }); |
14 | How do you delete documents? | Using the deleteOne() or deleteMany() methods | db.users.deleteOne({ name: "Charlie" }); |
15 | What is the _id field? | The primary key for each document, automatically generated if not provided | { "_id": ObjectId(""), "name": "Alice" } |
Intermediate MongoDB Answers
# | Question | Answer | Examples |
---|---|---|---|
1 | What is embedding? | Storing related data directly within a single document | A user document: { name: "Alice", address: { street: "123 Main St", city: "Anytown" } } |
2 | What is referencing? | Storing the _id of one document in another document (like a foreign key) | An order document: { order_id: 123, user_id: ObjectId(""), items: [...] } (referencing a user's ID) |
3 | When should you use embedding? | When related data is accessed together, is small, and has a one-to-one or few relationship | Embedding address details within a user document if they are always accessed together and don't change much. |
4 | When should you use referencing? | When related data is large, accessed independently, or has a one-to-many relationship | Referencing multiple orders from a user document, or referencing categories from a product document. |
5 | What is the Aggregation Framework? | A powerful tool for processing data and returning computed results | db.aggregate([ { $match: { age: { $gt: 25 } } }, { $group: { _id: "$city", count: { $sum: 1 } } } ]) |
6 | What is the $match stage in Aggregation? | Filters documents based on specified criteria | { $match: { status: "active" } } |
7 | What is the $group stage in Aggregation? | Groups documents by a specified key and performs aggregation functions | { $group: { _id: "$category", count: { $sum: 1 } } } |
8 | What is the $project stage in Aggregation? | Reshapes documents, selecting or excluding fields, creating new fields | { $project: { _id: 0, userName: "$name", userEmail: "$email" } } |
9 | What is the $sort stage in Aggregation? | Sorts documents based on specified fields | { $sort: { timestamp: -1 } } |
10 | What is the $lookup stage in Aggregation? | Performs a left outer join to another collection | { $lookup: { from: "orders", localField: "\_id", foreignField: "user_id", as: "userOrders" } } |
11 | How do you create an index? | Using the db.collection.createIndex() method | db.users.createIndex({ email: 1 }); |
12 | What is a unique index? | Ensures the values in the indexed field are unique | db.users.createIndex({ email: 1 }, { unique: true }); |
13 | What is a compound index? | An index on multiple fields | db.products.createIndex({ category: 1, price: -1 }); |
14 | What is the explain() method? | Provides details about how MongoDB executes a query (useful for performance analysis) | db.users.find({ name: "Alice" }).explain("executionStats"); |
15 | What is Write Concern? | Specifies the level of acknowledgment required for a write operation to be considered successful | db.users.insertOne({...}, { writeConcern: { w: 2 } }); (acknowledgment from primary and one secondary if available) |
Advanced MongoDB Answers
# | Question | Answer | Examples |
---|---|---|---|
1 | What is Sharding? | Distributing data across multiple servers (shards) to handle large datasets | Each shard holds a subset of the data based on a shard key. |
2 | What is a Shard Key? | The field(s) used to determine which shard a document will be stored on | db.shardCollection("orders", { shardKey: { user_id: 1 } }); |
3 | What is a Replica Set? | A group of MongoDB processes that maintain the same data set, providing high availability and redundancy | One primary node and multiple secondary nodes. |
4 | What is Read Concern? | Specifies the level of consistency required for a read operation | db.users.find({}, { readConcern: { level: 'local' } }); (can return slightly stale data) |
5 | What is Read Preference? | Specifies which node(s) in a replica set a read operation should target (e.g., primary, secondary) | db.users.find({}, { readPreference: 'secondaryPreferred' }); (reads from secondary if available, otherwise primary) |
6 | What are Transactions in MongoDB? | Allows atomic, multi-document operations across multiple collections and shards (introduced in 4.0) | const session = db.startSession(); session.startTransaction(); ... session.commitTransaction(); or session.abortTransaction(); |
7 | What is a TTL Index (Time-To-Live)? | Automatically removes documents from a collection after a specified time | db.logs.createIndex({ logTime: 1 }, { expireAfterSeconds: 3600 }); (removes after 1 hour) |
8 | What is a Text Index? | Enables full-text search on string fields | db.products.createIndex({ description: "text", tags: "text" }); |
9 | What is MongoDB Atlas Search? | A fully managed full-text search service built on Apache Lucene | Provides advanced search capabilities beyond basic text indexes |
10 | What are Change Streams? | A mechanism to capture real-time data changes (inserts, updates, deletes) in a collection | Allows applications to receive notifications about data changes |
11 | What is the Query Cache? | Caches aggregated query results to improve read performance | db.coll.aggregate([...]).cache(...) |
12 | How do you optimize MongoDB performance? | Use appropriate indexes, optimize queries, consider embedding vs. referencing, use sharding/replica sets | Use EXPLAIN, analyze query patterns, design efficient schemas |
13 | What is the Write Journal? | A mechanism where MongoDB stores data that is being written but not yet flushed to disk | Ensures data durability in case of crashes during a write operation |
14 | What is Oplog (Operation Log)? | A capped collection in a replica set that records all operations performed on the primary node | Used by secondary nodes to replicate changes from the primary |
15 | What is a Capped Collection? | A collection with a fixed maximum size or number of documents; older documents are automatically removed | db.createCollection("logs", { capped: true, size: 1000000 }); (1MB size) |