MongoDB Comprehensive Answers
π° Basic MongoDB Questions
-
What is MongoDB?
- MongoDB is a NoSQL, document-oriented database that provides high performance, high availability, and easy scalability. It stores data in flexible, JSON-like documents.
-
How is MongoDB different from RDBMS?
- MongoDB is schema-less, document-oriented, and horizontally scalable, while RDBMS is table-based, schema-enforced, and typically scaled vertically. MongoDB also doesn't support joins like RDBMS.
-
What is a document in MongoDB?
- A document is a set of key-value pairs (like JSON) that is the basic unit of data in MongoDB. Documents are stored in BSON format (binary JSON).
-
What is a collection?
- A collection is a group of MongoDB documents, similar to a table in RDBMS. Collections don't enforce a schema.
-
What is a database in MongoDB?
- A database is a physical container for collections. A single MongoDB server typically has multiple databases.
-
What are the advantages of using MongoDB?
- Flexible schema, horizontal scalability, high performance, rich query language, automatic sharding, replication, and support for multiple storage engines.
-
What data types are supported in MongoDB?
- String, Integer, Boolean, Double, Decimal128, ObjectId, Date, Timestamp, Array, Object (embedded document), Null, Binary data, Regular expression, JavaScript code, etc.
-
What is BSON?
- BSON (Binary JSON) is a binary-encoded serialization of JSON-like documents that MongoDB uses to store documents and make remote procedure calls.
-
What are indexes in MongoDB?
- Indexes are special data structures that store a small portion of the collection's data in an easy-to-traverse form, improving query performance.
-
What is a primary key in MongoDB?
- A primary key is a unique identifier for a document in a collection. In MongoDB, the
_id
field serves as the primary key.
- A primary key is a unique identifier for a document in a collection. In MongoDB, the
-
What is the default primary key in MongoDB?
- The default primary key is the
_id
field, which is automatically created if not specified, containing anObjectId
.
- The default primary key is the
-
How do you create a database in MongoDB?
use databaseName // The database is created when you first store data in it
-
How do you create a collection?
db.createCollection("collectionName") // Or implicitly by inserting a document db.collectionName.insert({...})
-
How do you insert a document?
db.collection.insertOne({name: "John", age: 30}) // or db.collection.insertMany([{...}, {...}])
-
How do you read documents?
db.collection.find() db.collection.findOne({age: {$gt: 25}})
-
How do you update documents?
db.collection.updateOne({name: "John"}, {$set: {age: 31}}) db.collection.updateMany({status: "active"}, {$inc: {views: 1}})
-
How do you delete a document?
db.collection.deleteOne({name: "John"}) db.collection.deleteMany({status: "inactive"})
-
How do you drop a collection?
db.collection.drop()
-
How do you drop a database?
db.dropDatabase()
-
What is
ObjectId
?- A 12-byte unique identifier typically used as the
_id
field in documents. It consists of:- 4-byte timestamp
- 5-byte random value
- 3-byte incrementing counter
- A 12-byte unique identifier typically used as the
π CRUD Operations
-
How do you insert multiple documents at once?
db.collection.insertMany([ {name: "Alice", age: 25}, {name: "Bob", age: 30} ])
-
What does the
find()
method return?- Returns a cursor to the documents that match the query criteria. You can iterate through the cursor to access the documents.
-
How can you filter documents in
find()
?
db.collection.find({age: {$gt: 25}, status: "active"})
-
What is the difference between
find()
andfindOne()
?-
find()
returns a cursor to multiple documents, whilefindOne()
returns a single document or null.
-
-
How do you update a nested field in a document?
db.collection.updateOne( {_id: 1}, {$set: {"address.city": "New York"}} )
-
What is
$set
,$unset
,$inc
,$push
,$pull
?-
$set
: Sets the value of a field -
$unset
: Removes a field -
$inc
: Increments a field's value -
$push
: Adds an item to an array -
$pull
: Removes items from an array that match a condition
-
-
How to replace an entire document?
db.collection.replaceOne( {_id: 123}, {name: "New Name", age: 40} )
-
What does
upsert
mean?- An operation that updates a document if it exists, or inserts a new document if it doesn't exist.
db.collection.updateOne( {name: "John"}, {$set: {age: 35}}, {upsert: true} )
-
How do you delete multiple documents?
db.collection.deleteMany({status: "inactive"})
-
How do you use
limit
,skip
, andsort
?
db.collection.find() .sort({age: -1}) // descending .skip(10) // skip first 10 .limit(5) // return 5
π§ Aggregation Framework
-
What is aggregation in MongoDB?
- A framework for performing data processing operations on documents, similar to SQL's GROUP BY and JOIN operations.
-
How is aggregation different from MapReduce?
- Aggregation uses a pipeline model with stages, while MapReduce uses JavaScript functions. Aggregation is generally faster for most operations.
-
What is the
$match
stage?- Filters documents to pass only those that match specified conditions to the next stage.
{$match: {status: "A"}}
-
What is the
$group
stage?- Groups documents by specified identifier and applies accumulator expressions.
{$group: {_id: "$department", total: {$sum: "$salary"}}}
-
What is the
$project
stage?- Reshapes each document by including, excluding, or adding fields.
{$project: {name: 1, department: 1, _id: 0}}
-
What does
$sort
do in aggregation?- Sorts all input documents and returns them in sorted order.
{$sort: {age: -1, name: 1}}
-
What is
$lookup
used for?- Performs a left outer join to another collection.
{ $lookup: { from: "orders", localField: "user_id", foreignField: "_id", as: "user_orders" } }
-
Explain
$unwind
.- Deconstructs an array field to output a document for each element.
{$unwind: "$tags"}
-
What is
$facet
used for?- Processes multiple aggregation pipelines within a single stage.
{ $facet: { "price": [{$group: {_id: null, avg: {$avg: "$price"}}}], "count": [{$count: "total"}] } }
-
How do you calculate total or average using aggregation?
db.sales.aggregate([ { $group: { _id: null, total: {$sum: "$amount"}, average: {$avg: "$amount"} } } ])
ποΈ Data Modeling
-
When to use embedding vs. referencing?
- Embed when:
- Data has a "contains" relationship
- One-to-many where the "many" are viewed with the parent
- Data doesn't change frequently
- Reference when:
- Data has many-to-many relationships
- Embedded data would result in duplication
- Child documents are large
- Embed when:
-
What is normalization and denormalization in MongoDB?
- Normalization: Splitting data into multiple collections with references (like RDBMS)
- Denormalization: Embedding related data in a single document (common in MongoDB)
-
What are schema-less documents?
- Documents in the same collection don't need to have the same fields or structure. However, in practice, applications usually enforce some schema.
-
What is a capped collection?
- Fixed-size collections that maintain insertion order. Once the size is reached, oldest documents are overwritten.
-
What is a TTL index?
- A special index that automatically removes documents after a specified time period.
db.logs.createIndex({createdAt: 1}, {expireAfterSeconds: 3600})
-
What is a covered query?
- A query where all fields in the query are part of an index, and all fields returned are in the same index, so MongoDB can fulfill the query without examining documents.
-
How do you model a one-to-many relationship?
- For small N: Embed the many side as an array
- For large N: Use references (array of ObjectIds)
-
How do you model a many-to-many relationship?
- Use an array of references in one or both collections:
// Books collection {_id: 1, title: "Book A", authors: [101, 102]} // Authors collection {_id: 101, name: "Author X", books: [1, 2]}
-
What are the benefits of embedding?
- Improved read performance (single read operation)
- Atomic writes for the entire document
- No joins needed
-
What are the trade-offs of referencing?
- Additional queries needed to resolve references
- More complex application code
- Potential consistency issues
βοΈ Indexing and Performance
-
What types of indexes does MongoDB support?
- Single field, Compound, Multikey (array), Text, Geospatial, Hashed, Wildcard, etc.
-
What is a compound index?
- An index on multiple fields.
db.users.createIndex({lastName: 1, firstName: 1})
-
What is a hashed index?
- An index that hashes the field value for equality queries (used mainly for sharding).
db.users.createIndex({username: "hashed"})
-
What is a text index?
- Supports text search queries on string content.
db.articles.createIndex({content: "text"})
-
How to create an index?
db.collection.createIndex({field: 1}) // 1 for ascending, -1 for descending
-
How to view all indexes on a collection?
db.collection.getIndexes()
-
How does indexing impact write performance?
- Each index adds overhead to write operations since MongoDB must update all indexes when data changes.
-
What is an index cardinality?
- The uniqueness of values in the indexed field. High cardinality means many unique values (good for selectivity).
-
How do you use explain plans?
db.collection.find({...}).explain("executionStats")
-
What does
explain()
show?- Query execution plan, indexes used, execution time, documents examined, etc.
π Security and Authentication
-
What are the different levels of authentication in MongoDB?
- SCRAM (default), x.509 certificates, LDAP, Kerberos
-
What is role-based access control?
- Users are assigned roles that define their privileges (read, readWrite, dbAdmin, etc.)
-
What is MongoDB's default port?
- 27017
-
How do you enable access control?
- Start mongod with
--auth
option or setsecurity.authorization
in config file
- Start mongod with
-
How do you create a user in MongoDB?
use admin db.createUser({ user: "admin", pwd: "password", roles: ["root"] })
-
How to encrypt data in MongoDB?
- Field-level encryption (Client-Side Field Level Encryption)
- Encryption at rest (Enterprise feature)
- TLS/SSL for network encryption
-
What is SCRAM authentication?
- Salted Challenge Response Authentication Mechanism (default authentication mechanism)
-
How do you secure a MongoDB deployment?
- Enable authentication
- Configure network encryption (TLS/SSL)
- Set up firewalls
- Regularly update MongoDB
- Follow principle of least privilege for users
-
What is IP whitelisting in MongoDB?
- Restricting access to MongoDB from specific IP addresses only.
-
What is MongoDB Atlas and how is it secured?
- MongoDB's fully-managed cloud database service, secured with:
- Network isolation (VPC peering)
- Encryption at rest and in transit
- IP whitelisting
- Auditing
- Automated patching
- MongoDB's fully-managed cloud database service, secured with:
βοΈ Replication and Sharding
-
What is replication in MongoDB?
- Maintaining multiple copies of data across different servers for high availability.
-
What is a replica set?
- A group of mongod processes that maintain the same data set, typically with one primary and multiple secondaries.
-
How does failover work in MongoDB?
- If the primary fails, an election occurs among secondaries to select a new primary.
-
What are the components of a replica set?
- Primary (accepts all writes)
- Secondaries (replicate primary's oplog)
- Arbiter (votes in elections but doesn't hold data)
-
What is an arbiter node?
- A lightweight mongod instance that votes in elections but doesn't store data (used to break ties).
-
What is sharding?
- Distributing data across multiple machines to support horizontal scaling.
-
What is a shard key?
- The field used to partition data across shards (must be chosen carefully).
-
How is data distributed in a sharded cluster?
- Data is partitioned into chunks based on the shard key range and distributed across shards.
-
When should you use sharding?
- When your data set exceeds the storage capacity of a single server
- When your active working set exceeds RAM
- When your write load exceeds a single server's capacity
-
What are chunk migrations?
- The process of moving chunks (ranges of shard key values) between shards to balance data distribution.
π§ͺ Transactions & Consistency
-
What are multi-document transactions?
- Operations that allow you to perform reads and writes across multiple documents in one or more collections with ACID guarantees.
-
Are MongoDB transactions ACID-compliant?
- Yes, since MongoDB 4.0 for replica sets and 4.2 for sharded clusters.
-
How do you start and commit a transaction?
const session = db.getMongo().startSession(); session.startTransaction(); try { db.collection1.insertOne({...}, {session}); db.collection2.updateOne({...}, {...}, {session}); session.commitTransaction(); } catch (error) { session.abortTransaction(); }
-
What are the limitations of MongoDB transactions?
- Maximum lifetime of 60 seconds
- Performance overhead
- Some operations cannot be used in transactions (like creating collections)
-
What is write concern?
- The level of acknowledgment requested from MongoDB for write operations.
db.collection.insertOne( {...}, {writeConcern: {w: "majority", wtimeout: 5000}} )
-
What is read concern?
- The level of isolation for read operations.
db.collection.find().readConcern("majority")
-
What is journaling in MongoDB?
- A write-ahead log that ensures data durability in case of a crash.
-
How does MongoDB handle concurrency?
- Uses document-level locking (since MongoDB 3.0) allowing concurrent operations on different documents.
π¦ Tools & Ecosystem
-
What is MongoDB Compass?
- The official GUI for MongoDB with schema visualization, query building, and performance analysis.
-
What is MongoDB Atlas?
- MongoDB's fully-managed cloud database service (DBaaS).
-
What is Mongoose?
- An ODM (Object Document Mapper) library for MongoDB and Node.js that provides schema validation and other features.
-
How do you connect to MongoDB using Mongoose?
const mongoose = require('mongoose'); mongoose.connect('mongodb://localhost/mydb', {useNewUrlParser: true});
-
What is the MongoDB shell?
- An interactive JavaScript interface to MongoDB for administration and data manipulation.
-
What is mongodump and mongorestore?
- Utilities for backing up (
mongodump
) and restoring (mongorestore
) MongoDB databases.
- Utilities for backing up (
-
What is MongoDB Ops Manager?
- A management platform for MongoDB deployments (monitoring, backup, automation).
-
What is GridFS in MongoDB?
- A specification for storing and retrieving large files (greater than 16MB) by splitting them into chunks.
-
What is a change stream?
- A real-time API that allows applications to subscribe to all data changes in MongoDB.
const changeStream = db.collection.watch(); changeStream.on('change', (change) => { ... });
-
What is the difference between MongoDB and Firebase?
- MongoDB is a general-purpose NoSQL database, while Firebase is a BaaS (Backend as a Service) with real-time capabilities and additional services.
-
What is MongoDB Realm?
- A mobile database and synchronization service (formerly Realm) now integrated with MongoDB.
-
How do you monitor MongoDB performance?
- Using tools like:
- MongoDB Atlas monitoring
- Ops Manager/Cloud Manager
- mongostat and mongotop commands
- Database Profiler
- explain() for query analysis
- Using tools like:
Top comments (1)
Thanks for sharing , you highlighted some great features of MongoDB!