MongoDB β
MongoDB is a popular document-oriented NoSQL database designed for modern applications. Unlike traditional relational databases, MongoDB stores data in flexible, JSON-like documents, making it ideal for handling diverse data types and rapidly evolving schemas.
π οΈ Getting Started β
π» Installation β
To start using MongoDB, you need to install it on your machine. Follow these steps for installation:
- Windows: Download the MongoDB installer from the MongoDB website and follow the installation wizard.
- macOS: Use Homebrew to install MongoDB with the command:
brew tap mongodb/brew && brew install mongodb-community
. - Linux: Use your distributionβs package manager or download the binaries from the MongoDB website.
πββ Running MongoDB β
After installation, you can start MongoDB with the mongod
command. To connect to your database, use the mongo
shell command.
π Basics β
π MongoDB Structure β
- Database: A container for collections.
- Collection: A group of MongoDB documents, akin to a table in relational databases.
- Document: A single record in a collection, stored in BSON (Binary JSON) format.
βοΈ CRUD Operations β
CRUD stands for Create, Read, Update, and Delete. Hereβs a basic overview:
- Create:
db.collection.insertOne({name:'Jay Pancholi', age: 29})
- Read:
db.collection.find({name:'Jay Pancholi'})
- Update:
db.collection.updateOne({name:'Jay Pancholi', age: 30})
- Delete:
db.collection.deleteOne({name:'Jay Pancholi'})
π Advanced β
π Indexing β
Indexes improve query performance by allowing MongoDB to quickly locate data without scanning the entire collection.
- Creating an Index:
db.collection.createIndex({id: 1})
- Compound Indexes: indexes on multiple fields to support complex queries
db.collection.createIndex({id: 1, age: 30})
π Aggregation Framework β
Aggregation operations process data records and return computed results. Use the aggregation pipeline for complex data transformations.
db.collection.aggregate([
{ $match: { age: { $gte: 30 } } },
{ $group: { _id: "$name", averageAge: { $avg: "$age" } } }
]);
ποΈ Schema Design β
Design your schema to balance flexibility and performance. Consider embedding related data in documents or using references between documents.
- Embedding: Useful for one-to-few relationships.
- Referencing: Suitable for one-to-many or many-to-many relationships.
π¦ Sharding β
Sharding distributes data across multiple servers to handle large datasets and high-throughput operations.
sh.enableSharding("myDatabase");
db.collection.createIndex({ shardKey: 1 });
sh.shardCollection("myDatabase.myCollection", { shardKey: 1 });
π Replication β
Recplication provides high availability by duplicating data across multiple servers
- Setting up Replica Sets:
rs.initiate({
_id: "rs0",
members: [
{ _id: 0, host: "localhost:27017" },
{ _id: 1, host: "localhost:27018" },
{ _id: 2, host: "localhost:27019" }
]
});
π‘ Best Practices β
𧩠Data Modeling β
Model your data based on access patterns and application requirements. Choose between embedding and referencing based on data relationships and query needs.
βοΈ Performance Tuning β
Monitor query performance and use indexing wisely. Analyze query execution plans and optimize slow queries.
π Security β
Implement authentication and authorization to control access to your MongoDB instances. Use TLS/SSL for encrypted communication.