MongoDB Interview Questions and Answers
What is MongoDB?
- MongoDB is an open-source NoSQL database system that stores data in flexible, JSON-like documents. It is designed for high scalability, performance, and availability.
What is the difference between SQL and NoSQL?
- SQL databases are relational databases that store data in tables and rows, using structured query language (SQL). NoSQL databases, like MongoDB, are non-relational and store data in formats like JSON, BSON, and key-value pairs.
What are the advantages of MongoDB?
- Scalability: Horizontal scaling through sharding.
- Flexible Schema: Data is stored in JSON-like documents, allowing flexible data models.
- High Availability: MongoDB supports replica sets, providing automatic failover and data redundancy.
- Performance: Optimized for high-performance operations.
- Rich Query Language: Supports powerful query features like aggregation, indexing, and full-text search.
What is a MongoDB document?
- A document in MongoDB is a basic unit of data, represented as a JSON-like object. It contains key-value pairs, with the values potentially being arrays or other documents.
What is the difference between a collection and a database in MongoDB?
- A database in MongoDB is a container for collections. A collection is a group of MongoDB documents, equivalent to a table in relational databases.
What are the types of data storage in MongoDB?
- Document storage: Data is stored in documents (BSON format).
- Collection storage: Groups of documents are stored in collections.
- GridFS: Used for storing large files like images, videos, or binary data.
What is BSON?
- BSON (Binary JSON) is a binary-encoded serialization format used by MongoDB to store data. It is similar to JSON but includes additional data types like `ObjectId` and `BinData`.
What is an ObjectId in MongoDB?
- ObjectId is a unique identifier for documents in MongoDB. It is automatically generated by MongoDB and consists of a 12-byte value.
What is Sharding in MongoDB?
- Sharding is the process of distributing data across multiple servers to support large-scale data storage and high-availability. It helps to horizontally scale the database.
What is replication in MongoDB?
- Replication in MongoDB involves copying data from one server (primary) to multiple servers (secondary) to ensure high availability and data redundancy. This is achieved using replica sets.
What is a replica set in MongoDB?
- A replica set is a group of MongoDB instances that maintain the same data set. One node is the primary node that accepts write operations, while the other nodes are secondary and replicate the data from the primary.
What is a primary key in MongoDB?
- The primary key in MongoDB is the `_id` field, which uniquely identifies each document in a collection. By default, MongoDB generates an ObjectId as the value of `_id` if not explicitly specified.
What are indexes in MongoDB?
- Indexes in MongoDB are used to improve the speed of query operations. They are built on one or more fields within documents and can be used for efficient searching, sorting, and aggregation.
How do you create an index in MongoDB?
- You can create an index using the `createIndex()` method on a collection. Example: `db.collection.createIndex({ field_name: 1 })` creates an ascending index on the `field_name` field.
What is the difference between a single and compound index in MongoDB?
- A single index is created on one field, while a compound index is created on multiple fields, which allows for better performance on queries that involve more than one field.
What are the types of indexes available in MongoDB?
- Single Field Index
- Compound Index
- Multikey Index
- Text Index
- Hashed Index
- Geospatial Index
- Wildcard Index
What is the aggregation framework in MongoDB?
- The aggregation framework in MongoDB is a powerful set of operations that allow for data transformation and processing. It includes stages like `$match`, `$group`, `$sort`, and `$project` for complex data manipulations.
What is a pipeline in MongoDB aggregation?
- A pipeline in MongoDB aggregation is a sequence of stages that process data, each stage performing an operation on the data. The result of one stage is passed to the next stage.
What are some common MongoDB queries?
- Find: `db.collection.find(query)`
- Insert: `db.collection.insert(document)`
- Update: `db.collection.update(query, update)`
- Delete: `db.collection.remove(query)`
What is the difference between `find()` and `findOne()` in MongoDB?
- `find()` returns a cursor to the documents that match the query criteria, whereas `findOne()` returns a single document matching the query.
What is the difference between `update()` and `updateOne()`?
- `update()` modifies multiple documents in a collection, while `updateOne()` modifies only the first document that matches the query criteria.
What is `upsert` in MongoDB?
- An `upsert` operation in MongoDB is an update operation that inserts a new document if no documents match the query criteria.
What is a capped collection in MongoDB?
- A capped collection in MongoDB is a fixed-size collection that automatically overwrites old documents when it reaches its size limit. It is typically used for logging purposes.
What is the purpose of `$in` operator in MongoDB?
- The `$in` operator is used to find documents where the value of a field matches any value in a specified array. Example: `db.collection.find({ field: { $in: [value1, value2] } })`.
What is the `$and` operator in MongoDB?
- The `$and` operator is used to match documents that satisfy multiple conditions. Example: `db.collection.find({ $and: [{ age: { $gt: 18 } }, { status: 'active' }] })`.
How does MongoDB handle transactions?
- MongoDB supports multi-document transactions, which allow for atomic operations across multiple documents and collections. Transactions are ACID-compliant and provide rollback and commit functionality.
What is the `findAndModify` operation in MongoDB?
- The `findAndModify` operation in MongoDB allows you to find a document and modify it in a single atomic operation. It can be used to update, insert, or remove documents.
What is the `distinct()` method in MongoDB?
- The `distinct()` method in MongoDB is used to find all the distinct values for a specified field in a collection.
What is GridFS in MongoDB?
- GridFS is a specification for storing and retrieving large files, such as images, videos, and audio files, in MongoDB. It splits files into chunks and stores them in separate documents.
What is the purpose of `MongoDB Atlas`?
- MongoDB Atlas is a fully managed cloud service for MongoDB that provides automated deployment, scaling, monitoring, and backups, eliminating the need for on-premise database management.
How do you perform data backup in MongoDB?
- You can back up MongoDB using the `mongodump` utility, which creates a binary export of the data from a MongoDB instance, and `mongorestore` to restore the data.
What are some methods for securing MongoDB?
- Enable authentication using username and password.
- Use role-based access control (RBAC) to restrict permissions.
- Enable encryption at rest and in transit.
- Use firewalls to control network access.