Installation (Ubuntu 22.04)
Bash
# GPG
curl -fsSL https://www.mongodb.org/static/pgp/server-7.0.asc | \
sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg --dearmor
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] \
https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse" | \
sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
sudo apt update && sudo apt install -y mongodb-org
sudo systemctl enable --now mongod
# mongosh
mongoshDatabases and Collections Basics
JavaScript
// DB
use mydb
// list
show collections
// ( when )
db.createCollection("users")CRUD Basics
JavaScript
// ── Create ──────────────────────────────────────
db.users.insertOne({
name: "Alice",
email: "alice@example.com",
age: 30,
tags: ["admin", "active"],
createdAt: new Date()
})
db.users.insertMany([
{ name: "Bob", email: "bob@example.com", age: 25 },
{ name: "Carol", email: "carol@example.com", age: 35 }
])
// ── Read ────────────────────────────────────────
db.users.find //
db.users.find({ age: { $gt: 25 } }) //
db.users.findOne({ email: "alice@example.com" })
//
db.users.find({}, { name: 1, email: 1, _id: 0 })
// ··
db.users.find().sort({ age: -1 }).limit(10).skip(20)
// ── Update ──────────────────────────────────────
db.users.updateOne(
{ email: "alice@example.com" },
{ $set: { age: 31 }, $push: { tags: "editor" } }
)
db.users.updateMany(
{ age: { $lt: 18 } },
{ $set: { status: "minor" } }
)
// upsert
db.users.updateOne(
{ email: "dave@example.com" },
{ $set: { name: "Dave" } },
{ upsert: true }
)
// ── Delete ──────────────────────────────────────
db.users.deleteOne({ email: "bob@example.com" })
db.users.deleteMany({ status: "inactive" })Query Operators
JavaScript
// compare
{ age: { $gt: 25, $lte: 40 } } // 25 40
{ status: { $in: ["active", "pending"] } }
{ status: { $nin: ["banned"] } }
//
{ $and: [{ age: { $gt: 20 } }, { age: { $lt: 40 } }] }
{ $or: [{ name: "Alice" }, { name: "Bob" }] }
//
{ tags: "admin" } //
{ tags: { $all: ["admin", "active"] } }
//
{ name: { $regex: /^Al/i } }Indexes
JavaScript
//
db.users.createIndex({ email: 1 }, { unique: true })
//
db.users.createIndex({ age: -1, name: 1 })
// TTL (30 after )
db.sessions.createIndex({ createdAt: 1 }, { expireAfterSeconds: 2592000 })
// list
db.users.getIndexes()
// run
db.users.find({ email: "alice@example.com" }).explain("executionStats")Aggregation Pipeline
JavaScript
//
db.users.aggregate([
{ $match: { status: "active" } }, //
{ $group: {
_id: "$department",
avgAge: { $avg: "$age" },
count: { $sum: 1 }
}},
{ $sort: { count: -1 } },
{ $limit: 5 }
])
// $lookup —
db.orders.aggregate([
{ $lookup: {
from: "users",
localField: "userId",
foreignField: "_id",
as: "user"
}},
{ $unwind: "$user" },
{ $project: { orderId: 1, "user.name": 1, total: 1 } }
])Backup and Restore
Bash
# backup
mongodump --out /backup/$(date +%Y%m%d)
# DB backup
mongodump --db mydb --out /backup/
#
mongorestore /backup/20250527/
#
mongodump --db mydb --collection users --out /backup/
mongorestore --db mydb --collection users /backup/mydb/users.bsonEditorial note
AI tools may assist drafting. Check the evidence in each guide for whether commands were run and which environment was used. Confirm the current docs for your version before applying instructions, and email us about errors. email us
Topic-related official docs (auto-linked · not per-claim sources)MongoDB manual ↗
Questions & answers
Ask a question about this guide. We'll review and reply.