Example 1 — Sample Data
Five products with name, category, status, price, and cost. We load them into a collection named products.
db.products.drop()
db.products.insertMany([
{ _id: 1, name: "Notebook", category: "Books", status: "active", price: 12, cost: 5 },
{ _id: 2, name: "Keyboard", category: "Gadgets", status: "active", price: 45, cost: 20 },
{ _id: 3, name: "Mug", category: "Home", status: "active", price: 8, cost: 2 },
{ _id: 4, name: "Old HD", category: "Gadgets", status: "retired", price: 40, cost: 10 },
{ _id: 5, name: "Cable", category: "Gadgets", status: "active", price: 6, cost: 8 }
]) How the Data Is Stored in MongoDB
After the insert, MongoDB keeps five documents in products. Run find() to see them:
db.products.find() Each product document uses these fields:
| Field in MongoDB | Example | Plain English |
|---|---|---|
name | "Keyboard" | Product label |
category | "Gadgets" | Grouping for filters like $in |
status | "active" / "retired" | Whether the item is still sold |
price | 45 | Sale price (number) |
cost | 20 | Cost basis for margin checks |
products (5 rows):
name category status price cost
-------- -------- ------- ----- ----
Notebook Books active 12 5
Keyboard Gadgets active 45 20
Mug Home active 8 2
Old HD Gadgets retired 40 10
Cable Gadgets active 6 8
Next examples filter this same set with $match. How It Works
- Reset once.
drop()clears old rows;insertManyloads the five products. - Stable ids.
_id1–5 makes it easy to spot documents in output. - find() confirms storage. You see the same fields the pipeline will read.
- Reuse. Examples 2–4 assume this data is already in
products.

