1. What is MongoDB and why use MongoDB?
MongoDB is an open-source, document-oriented NoSQL database designed for scalability and flexibility. Unlike traditional relational databases, MongoDB stores data in JSON-like documents (BSON format). It’s used for applications that require high availability, scalability, and flexibility, such as real-time analytics, content management, IoT, and e-commerce systems.
2. What are some key features of MongoDB?
Key features of MongoDB include:
- Open-source
- Scalable (both horizontally and vertically)
- High availability with replica sets
- Flexible schema (NoSQL, document-based storage)
- Supports automatic sharding (horizontal scaling)
- Built-in replication for fault tolerance
- Built-in file storage with GridFS
- Aggregation framework for real-time analytics
3. What is BSON in MongoDB?
BSON (Binary JSON) is a binary representation of JSON-like documents. It allows MongoDB to store data efficiently, supporting more complex data types like dates, binary data, and embedded documents that standard JSON does not. BSON is used internally for storing documents and making queries faster and more efficient.
4. What are the types of data MongoDB stores?
MongoDB can store the following types of data:
- Numbers (Int32, Int64, Double)
- Strings
- Arrays
- Binary Data
- ObjectId
- Date and Timestamp
- Boolean
- Null
- Regular Expression
- MinKey, MaxKey
- Embedded Documents
5. Where is MongoDB data stored?
By default, MongoDB stores its data in the /data/db directory on the local filesystem. This can be changed via configuration files. Data is stored in BSON files, which the MongoDB storage engine uses to manage and retrieve documents.
6. How to check MongoDB version?
To check MongoDB server version: mongod --version
To check MongoDB shell version: mongo --version
7. What is sharding in MongoDB?
Sharding is a method for distributing data across multiple machines. It helps MongoDB handle large datasets and traffic by horizontally scaling, making it possible to distribute data across several machines to ensure better load balancing and performance. Sharding involves a shard key that determines how the data is distributed.
8. How to create a collection in MongoDB?
A collection can be created using the db.createCollection() method:
db.createCollection("myCollection")
Collections in MongoDB are usually created automatically when you insert a document into them.
9. What is a replica set in MongoDB?
A replica set is a group of MongoDB servers that maintain the same data set. It ensures high availability by keeping multiple copies (replicas) of data across different servers. The primary server handles all writes, while secondary servers replicate the data from the primary server.
10. What are MongoDB indexes?
MongoDB indexes are data structures that improve the speed of data retrieval operations. Indexes can be created on one or more fields in a collection and are used to efficiently search and sort data, rather than scanning every document in a collection.
11. How do MongoDB indexes work?
MongoDB indexes work by creating a data structure (typically a B-tree) that stores field values and pointers to the corresponding documents. When a query is executed, MongoDB uses the index to quickly locate the required documents, rather than scanning the entire collection.
12. How MongoDB is scalable?
MongoDB is horizontally scalable through sharding, which splits data into smaller, more manageable chunks distributed across different machines. This allows MongoDB to scale out by adding more servers to handle increasing data loads. Vertical scaling (increasing the power of a single machine) is also supported but less common than horizontal scaling.
13. What is ObjectID in MongoDB?
ObjectID is a unique identifier used by MongoDB for documents. It is a 12-byte identifier that consists of:
- 4 bytes for timestamp (seconds since Unix epoch)
- 5 bytes for machine identifier
- 3 bytes for process ID
- 3 bytes for a random value
This ensures uniqueness across different machines and processes.
14. What is MongoDB Compass?
MongoDB Compass is a graphical user interface (GUI) tool for MongoDB that allows users to explore, visualize, and manipulate MongoDB data. It helps developers and administrators interact with the database through a user-friendly interface without needing to write complex queries.
15. What is Scaling in MongoDB?
MongoDB supports both horizontal and vertical scaling:
- Horizontal scaling (Sharding): Distributes data across multiple machines to handle large datasets and traffic.
- Vertical scaling: Adds more resources (CPU, RAM, Storage) to a single machine.
16. What is the Pretty method in MongoDB?
The pretty() method is used to format the output of queries for better readability. It’s often used in the MongoDB shell to display results in a more user-friendly, human-readable format:
db.collection.find().pretty()
17. How to use GridFS in MongoDB?
GridFS is a specification for storing large files (like images, videos, etc.) in MongoDB. It breaks the file into smaller chunks and stores them in two collections: fs.files and fs.chunks. You can use the GridFSBucket API in MongoDB to interact with GridFS, such as uploading, downloading, and managing files.
18. What is __v in MongoDB?
The __v field is used by Mongoose (an ODM for MongoDB) to track the version of a document. It is a mechanism for versioning and helps prevent race conditions in applications. This field is not part of MongoDB itself, but it is added by Mongoose when using its versioning feature.
19. What is Aggregation in MongoDB?
Aggregation is the process of transforming and combining data in MongoDB to get more complex results, such as grouping, filtering, sorting, and reshaping documents. The aggregation framework in MongoDB uses stages such as $match, $group, $sort, and $project to manipulate data.
20. How to perform joins in MongoDB?
MongoDB is a NoSQL database, and traditionally, it does not support joins like relational databases. However, MongoDB 3.2 and later support the $lookup stage in aggregation pipelines, which allows you to perform left outer joins to combine documents from different collections.
21. What is the difference between MongoDB and MySQL?
MongoDB is a NoSQL, document-oriented database, whereas MySQL is a relational database management system (RDBMS). MongoDB offers flexibility with its schema design, allowing you to store data in JSON-like documents, while MySQL relies on tables with predefined schemas. MongoDB provides horizontal scaling (through sharding), while MySQL scales vertically. MongoDB is better suited for big data and real-time applications.
22. How can you ensure data integrity in MongoDB?
Data integrity in MongoDB can be ensured using the following techniques:
- Replica Sets to ensure data availability and consistency across multiple nodes.
- Transactions (introduced in MongoDB 4.0) to ensure ACID (Atomicity, Consistency, Isolation, Durability) properties for multi-document operations.
- Data Validation using schema validation rules to enforce data integrity on the application level.
23. How can you handle errors in MongoDB?
Errors in MongoDB can be handled by:
- Using try-catch blocks in your application code to catch errors.
- Checking error codes and messages in MongoDB's error response.
- Using Write Concern to ensure write operations are successful.
- Implementing Retryable Writes to automatically retry operations after a network error or replica set failover.