Example 1 — Sample Data
Imagine four orders. Quantity is stored as a string (or null). We save them in a collection named orders.
db.orders.insertMany([
{ _id: 1, item: "apple", qty: "5" },
{ _id: 2, item: "orange", qty: "10" },
{ _id: 3, item: "banana", qty: "abc" },
{ _id: 4, item: "pear", qty: null }
]); How the Data Is Stored in MongoDB
After the insert, MongoDB keeps four documents in orders. Run find() to see them:
db.orders.find() Read those documents like this:
| Field in MongoDB | Example | Plain English |
|---|---|---|
_id | 1 … 4 | Order id for this lesson |
item | "apple" | Product name |
qty | "5" or null | Quantity stored as string / null (dirty data) |
Convert qty → int:
"5" → 5 (success)
"10" → 10 (success)
"abc" → error (needs onError)
null → null (needs onNull)
Goal: get numbers we can add / compare. How It Works
- Insert once.
insertManyadds four order documents. - MongoDB stores types as written.
"5"is a string;nullis null—not an int yet. - find() shows the same shape. What you inserted is what you get back—ready for
$convertnext.

