Example 1 — Sample Data
Imagine four receipts in a transactions collection. Each document has an amount and a category.
db.transactions.insertMany([
{ amount: 100, category: "food" },
{ amount: 150, category: "food" },
{ amount: 200, category: "travel" },
{ amount: 50, category: "food" }
]); How the Data Is Stored in MongoDB
After the insert, MongoDB keeps 4 documents in transactions. Run find() to see them:
db.transactions.find() Read each document like this:
| Field in MongoDB | Meaning | Example |
|---|---|---|
amount | Money spent on this purchase | 100 |
category | What the purchase was for | "food" |
Same four purchases:
food: 100, 150, 50 → average later
travel: 200 → average later How It Works
- Insert once. The sample command adds 4 documents to
transactions. - MongoDB stores fields as written. Each document keeps the same field names and values you inserted.
- find() shows the same shape. What you inserted is what you get back—ready for
$avgin the next examples.
Quick math: all amounts average to 500 / 4 = 125; food only 300 / 3 = 100; travel 200 / 1 = 200.

