MongoDB Tutorial

Document data with MongoDB — collections, queries, aggregation, and indexes.

1 min read 127 words Updated Mar 7, 2026

MongoDB stores JSON-like documents. It scales horizontally and fits flexible or evolving schemas.

Inserting documents

use shop;
db.products.insertMany([
  { name: "Keyboard", price: 49, tags: ["tech"] },
  { name: "Mug", price: 12, tags: ["home"] }
]);

Querying

db.products.find({ price: { $gt: 20 } }).sort({ price: -1 });
db.products.find({ tags: "tech" });

Updating

db.products.updateOne(
  { name: "Mug" },
  { $set: { price: 14 }, $push: { tags: "sale" } }
);

Aggregation pipeline

db.products.aggregate([
  { $match: { price: { $gt: 10 } } },
  { $group: { _id: null, avg: { $avg: "$price" }, n: { $sum: 1 } } }
]);
Concept SQL analog
collection table
document row
field column
_id primary key

Embed related data when it’s read together; reference it when it’s shared across many documents.