MongoDB
MongoDB is a NoSQL, document-oriented database that stores data in flexible, JSON-like documents. It is schema-less, scalable, and allows easy querying and indexing.
Key Concepts
Document-Oriented:
- Data is stored in BSON (Binary JSON) format.
- Each record in MongoDB is a document (like a row in relational databases).
- Documents consist of key-value pairs (similar to JSON objects).
Database, Collections, and Documents:
- Database: A container for collections.
- Collection: A group of MongoDB documents (like a table).
- Document: A record with data in key-value pairs (like a row).
Core Features
Schema-Less:
- Collections can hold documents of different structures.
- No predefined schema (flexible data storage).
Horizontal Scaling:
- MongoDB supports sharding (partitioning data across multiple servers).
Replication:
- Provides high availability with replica sets (data is replicated across multiple servers).
Indexing:
- MongoDB supports indexing to optimize query performance.
jsdb.collection.createIndex({ field: 1 }); // Create index on a field
MongoDB CRUD Operations
Create:
- Insert a document:js
db.collection.insertOne({ name: "Alice", age: 25 }); db.collection.insertMany([{ name: "Bob" }, { name: "Charlie" }]);
- Insert a document:
Read:
- Find documents:js
db.collection.find(); // Retrieve all documents db.collection.find({ name: "Alice" }); // Filter documents by a condition db.collection.find({}, { name: 1, age: 1 }); // Projection (select fields)
- Find with operators:js
db.collection.find({ age: { $gt: 20 } }); // $gt: greater than
- Find documents:
Update:
- Update a document:js
db.collection.updateOne({ name: "Alice" }, { $set: { age: 26 } }); db.collection.updateMany( { age: { $lt: 30 } }, { $set: { status: "active" } } );
- Update a document:
Delete:
- Delete documents:js
db.collection.deleteOne({ name: "Bob" }); db.collection.deleteMany({ age: { $lt: 30 } });
- Delete documents:
MongoDB Aggregation
Aggregation Framework:
- Used for performing complex data processing and transformations (e.g., filtering, grouping, sorting).
jsdb.collection.aggregate([ { $match: { age: { $gt: 20 } } }, // Filter stage { $group: { _id: "$status", total: { $sum: 1 } } }, // Group stage ]);
Aggregation Pipeline:
- The pipeline allows multiple stages for transforming data.
- Common stages:
$match
: Filters documents.$group
: Groups documents by a key.$project
: Reshapes documents.$sort
: Sorts documents.$limit
: Limits the number of documents.
Indexes in MongoDB
Creating Indexes:
- Improve query performance by indexing frequently queried fields.
jsdb.collection.createIndex({ name: 1 }); // 1 for ascending, -1 for descending
Compound Index:
- Create an index on multiple fields.
jsdb.collection.createIndex({ name: 1, age: -1 });
Text Indexes:
- Used for text search on string fields.
jsdb.collection.createIndex({ description: "text" });
Query Operators
Comparison Operators:
$eq
: Equal to.$ne
: Not equal to.$gt
: Greater than.$lt
: Less than.$in
: Matches any value in an array.
jsdb.collection.find({ age: { $gt: 20 } }); db.collection.find({ name: { $in: ["Alice", "Bob"] } });
Logical Operators:
$or
: Matches documents where at least one condition is true.$and
: Matches documents where all conditions are true.$not
: Inverts the condition.
jsdb.collection.find({ $or: [{ age: { $lt: 25 } }, { name: "Bob" }] });
Update Operators:
$set
: Modifies or adds fields.$inc
: Increments a field by a given value.$unset
: Removes a field.
jsdb.collection.updateOne({ name: "Alice" }, { $set: { age: 26 } }); db.collection.updateOne({ name: "Alice" }, { $inc: { age: 1 } });
MongoDB with Node.js
Connecting to MongoDB:
- Use the
mongodb
package to connect to a MongoDB instance from a Node.js app.
bashnpm install mongodb
- Use the
Example of Connecting:
jsconst { MongoClient } = require("mongodb"); const uri = "mongodb://localhost:27017"; const client = new MongoClient(uri); async function run() { try { await client.connect(); console.log("Connected to MongoDB"); } finally { await client.close(); } } run().catch(console.dir);
Basic CRUD with Node.js:
jsconst collection = client.db("mydb").collection("users"); // Insert await collection.insertOne({ name: "Alice", age: 25 }); // Find const users = await collection.find({}).toArray(); // Update await collection.updateOne({ name: "Alice" }, { $set: { age: 26 } }); // Delete await collection.deleteOne({ name: "Alice" });
Working with Mongoose
What is Mongoose?
- Mongoose is an ODM (Object Data Modeling) library for MongoDB.
- Provides a structured schema and validation for MongoDB documents.
Connecting with Mongoose:
jsconst mongoose = require("mongoose"); mongoose.connect("mongodb://localhost:27017/mydb", { useNewUrlParser: true, useUnifiedTopology: true, });
Creating a Schema:
jsconst userSchema = new mongoose.Schema({ name: String, age: Number, }); const User = mongoose.model("User", userSchema);
CRUD Operations with Mongoose:
Create:
jsconst newUser = new User({ name: "Alice", age: 25 }); newUser.save();
Read:
jsUser.find({}, (err, users) => { console.log(users); });
Update:
jsUser.updateOne({ name: "Alice" }, { age: 26 });
Delete:
jsUser.deleteOne({ name: "Alice" });
Transactions
ACID Transactions:
- MongoDB supports multi-document transactions to ensure atomicity and consistency.
jsconst session = client.startSession(); session.startTransaction(); try { await collection.insertOne({ name: "Alice" }, { session }); await collection.updateOne( { name: "Bob" }, { $set: { age: 30 } }, { session } ); await session.commitTransaction(); } catch (err) { await session.abortTransaction(); } finally { session.endSession(); }
Indexes and Performance Optimization
Optimizing Queries:
- Use indexes to improve query speed.
- Use
$explain()
to analyze query performance.
Indexes in Mongoose:
jsuserSchema.index({ name: 1 });
Security Practices
Authentication and Authorization:
- Use built-in MongoDB authentication and roles to restrict access.
- Always authenticate using strong credentials.
Data Encryption:
- Encrypt data in transit using SSL/TLS.
- Enable encryption at rest for sensitive data.
Backup and Restore
Backup:
- Use
mongodump
to create backups.
bashmongodump --db mydb --out /backup/dir
- Use
Restore:
- Use
mongorestore
to restore from backups.
bashmongorestore /backup/dir
- Use
Summary
Flexible and Scalable: Stores data in flexible, JSON-like documents.
CRUD Operations: Insert, find, update, and delete documents easily.
Aggregation: Use pipelines to transform and analyze data.
Indexes: Optimize query performance with indexes.
Security: Ensure proper authentication, encryption, and backup practices.
Mongoose: Simplifies MongoDB operations in Node.js with schema validation.