MongoDB Cheat Sheet – Quick Reference!

Listen to this Post

2025-02-16

📌 Basic Commands

✅ `show dbs` – List all databases

✅ `use database_name` – Switch to a database

✅ `db.createCollection(“collection_name”)` – Create a collection

✅ `db.dropDatabase()` – Delete current database

📄 CRUD Operations

🔹 Insert Data

✅ `db.users.insertOne({name: “John”, age: 25})`

✅ `db.users.insertMany([{name: “Alice”}, {name: “Bob”}])`

🔹 Read Data

✅ `db.users.find()` – Retrieve all documents

✅ `db.users.find({age: {$gt: 20}})` – Find users older than 20

✅ `db.users.find().sort({age: -1})` – Sort by age (descending)

🔹 Update Data

✅ `db.users.updateOne({name: “John”}, {$set: {age: 30}})`

✅ `db.users.updateMany({}, {$set: {status: “active”}})`

🔹 Delete Data

✅ `db.users.deleteOne({name: “Alice”})`

✅ `db.users.deleteMany({age: {$lt: 18}})`

🔗 Indexing & Aggregation

✅ `db.users.createIndex({name: 1})` – Create an index on the “name” field
✅ `db.users.aggregate([{$group: {_id: “$status”, count: {$sum: 1}}}])` – Group by status

📌 Extra Tips

✅ `db.users.countDocuments()` – Count documents in a collection

✅ `db.users.distinct(“name”)` – Get unique values of a field

✅ `db.users.find().limit(5)` – Limit results

💡 Pro Tip: Always index frequently queried fields for better performance!

What Undercode Say

MongoDB is a powerful NoSQL database that offers flexibility and scalability for modern applications. Mastering its commands and operations is essential for developers working with data-driven applications. Here are some additional Linux and Windows commands to complement your MongoDB skills:

  • Linux Commands:
    – `sudo systemctl start mongod` – Start MongoDB service on Linux.
    – `sudo systemctl status mongod` – Check MongoDB service status.
    – `mongo –host 127.0.0.1:27017` – Connect to MongoDB instance.
    – `mongodump –db database_name –out /backup/path` – Backup a MongoDB database.
    – `mongorestore –db database_name /backup/path` – Restore a MongoDB database.

  • Windows Commands:
    – `net start MongoDB` – Start MongoDB service on Windows.
    – `net stop MongoDB` – Stop MongoDB service on Windows.
    – `mongo.exe –host 127.0.0.1:27017` – Connect to MongoDB instance.
    – `mongodump.exe –db database_name –out C:\backup\path` – Backup a MongoDB database.
    – `mongorestore.exe –db database_name C:\backup\path` – Restore a MongoDB database.

For advanced MongoDB operations, consider exploring its aggregation framework, which allows for complex data transformations and analysis. Additionally, always ensure your database is secure by enabling authentication and encryption.

For further reading, check out the official MongoDB documentation: MongoDB Docs.

By combining MongoDB with Linux or Windows commands, you can streamline your database management and enhance your development workflow. Whether you’re building a small project or a large-scale application, MongoDB’s versatility makes it a go-to choice for developers worldwide.

References:

Hackers Feeds, Undercode AIFeatured Image