DEV Community

Quipoin
Quipoin

Posted on

🍃 MongoDB Cheatsheet for Developers (Quick Reference)


If you're working with MongoDB, remembering all database commands can be challenging.

This MongoDB Cheatsheet provides a quick reference for the most commonly used commands such as creating databases, inserting documents, querying data, updating records, and deleting documents.

Save this guide and use it while building backend applications.

1. Connect to MongoDB

Start the MongoDB shell:

mongo

Connect using a connection string:

mongodb://localhost:27017

2. Show Databases

List all databases:

show dbs

Switch to a database:

use myDatabase

3. Collections

Show collections in a database:

show collections

Create a collection:

db.createCollection("users")

4. Insert Documents

Insert a single document:

db.users.insertOne({
name: "John",
age: 25
})

Insert multiple documents:

db.users.insertMany([
{ name: "Alice" },
{ name: "Bob" }
])

5. Query Documents

Find all documents:

db.users.find()

Find a single document:

db.users.findOne()

Find with a condition:

db.users.find({ age: 25 })

6. Update Documents

Update one document:

db.users.updateOne(
{ name: "John" },
{ $set: { age: 30 } }
)

Update multiple documents:

db.users.updateMany(
{ age: 25 },
{ $set: { status: "active" } }
)

7. Delete Documents

Delete one document:

db.users.deleteOne({ name: "John" })

Delete multiple documents:

db.users.deleteMany({ age: 25 })

Learning **MongoDB becomes much easier when you keep a cheatsheet like this nearby.

This guide covers the most commonly used commands developers use while building modern backend applications.

Use it as a quick reference while working with databases.

Read Tutorial: https://www.quipoin.com/tutorial/tutorial-list

Top comments (0)