This post is a comprehensive guide to the most important MongoDB commands and operations. It covers basic queries, CRUD operations, indexing, aggregation, and more advanced concepts.
1. MongoDB Basics
Connecting to MongoDB
mongo --host <hostname> --port <port> -u <username> -p <password> --authenticationDatabase <authDB>
Show Databases
show dbs;
Use a Database
use myDatabase;
Show Collections
show collections;
2. CRUD Operations
Create a Collection
db.createCollection("myCollection");
Insert Documents
db.myCollection.insertOne({ name: "John", age: 30 });
db.myCollection.insertMany([{ name: "Jane", age: 25 }, { name: "Doe", age: 22 }]);
Read Documents
db.myCollection.find();
db.myCollection.find({ name: "John" });
Update Documents
db.myCollection.updateOne({ name: "John" }, { $set: { age: 31 } });
db.myCollection.updateMany({ age: { $lt: 30 } }, { $set: { status: "young" } });
Delete Documents
db.myCollection.deleteOne({ name: "John" });
db.myCollection.deleteMany({ age: { $lt: 25 } });
3. Querying Documents
Basic Queries
db.myCollection.find({ name: "John" });
db.myCollection.find({ age: { $gt: 25 } });
Projection
db.myCollection.find({}, { name: 1, age: 1 });
Sorting
db.myCollection.find().sort({ age: -1 });
Limiting and Skipping
db.myCollection.find().limit(5);
db.myCollection.find().skip(5).limit(5);
4. Updating Documents
Update Operators
- $set: Sets the value of a field
db.myCollection.updateOne({ name: "John" }, { $set: { age: 31 } });
- $unset: Removes a field
db.myCollection.updateOne({ name: "John" }, { $unset: { age: "" } });
- $inc: Increments the value of a field
db.myCollection.updateOne({ name: "John" }, { $inc: { age: 1 } });
- $push: Adds an item to an array
db.myCollection.updateOne({ name: "John" }, { $push: { hobbies: "reading" } });
5. Deleting Documents
Delete Operators
- deleteOne: Deletes a single document
db.myCollection.deleteOne({ name: "John" });
- deleteMany: Deletes multiple documents
db.myCollection.deleteMany({ age: { $lt: 25 } });
6. Indexes
Creating Indexes
db.myCollection.createIndex({ name: 1 });
Viewing Indexes
db.myCollection.getIndexes();
Dropping Indexes
db.myCollection.dropIndex("name_1");
7. Aggregation
Basic Aggregation
db.myCollection.aggregate([
{ $match: { status: "active" } },
{ $group: { _id: "$age", total: { $sum: 1 } } }
]);
Common Aggregation Stages
- $match: Filters documents
{ $match: { status: "active" } }
- $group: Groups documents by a specified field
{ $group: { _id: "$age", total: { $sum: 1 } } }
- $sort: Sorts documents
{ $sort: { total: -1 } }
- $project: Reshapes documents
{ $project: { name: 1, age: 1 } }
8. Data Modeling
Embedding Documents
let post = {
title: "Post Title",
content: "Post Content",
comments: [
{ user: "John", comment: "Great post!" },
{ user: "Jane", comment: "Thanks for sharing!" }
]
};
Referencing Documents
let user = { name: "John" };
let post = { title: "Post Title", content: "Post Content", userId: user._id };
9. Replication
Setting Up a Replica Set
rs.initiate();
rs.add("mongodb1.example.net:27017");
rs.add("mongodb2.example.net:27017");
rs.add("mongodb3.example.net:27017");
Checking Replica Set Status
rs.status();
10. Sharding
Enabling Sharding
sh.enableSharding("myDatabase");
Sharding a Collection
sh.shardCollection("myDatabase.myCollection", { shardKey: 1 });
11. Transactions
Starting a Transaction
const session = db.getMongo().startSession();
session.startTransaction();
Committing a Transaction
session.commitTransaction();
session.endSession();
Aborting a Transaction
session.abortTransaction();
session.endSession();
12. Security
Creating a User
db.createUser({
user: "myUser",
pwd: "myPassword",
roles: [{ role: "readWrite", db: "myDatabase" }]
});
Authenticating a User
db.auth("myUser", "myPassword");
13. Best Practices
- Use Indexes: Ensure that your queries are efficient by using indexes.
- Data Modeling: Choose the right data model (embedding vs. referencing) based on your use case.
- Backup Regularly: Regularly backup your data to prevent data loss.
- Monitor Performance: Use monitoring tools to keep an eye on the performance of your MongoDB instance.
- Secure Your Database: Implement proper authentication and authorization mechanisms.
Conclusion
This post summarizes key concepts, commands, and operations for working with MongoDB. Keep this as a quick reference while you're working with MongoDB!
Top comments (0)