Example 1 — Sample Data
Imagine five products. Most have a colors array; one is missing the field; one stores a string by mistake.
db.inventory.insertMany([
{ _id: 1, item: "ABC1", colors: ["blue", "black", "red"] },
{ _id: 2, item: "ABC2", colors: ["purple"] },
{ _id: 3, item: "XYZ1", colors: [] },
{ _id: 4, item: "ZZZ1" },
{ _id: 5, item: "ZZZ2", colors: "blue,red" }
]); How the Data Is Stored in MongoDB
After the insert, MongoDB keeps five documents. Run find() to see them:
db.inventory.find() Read one document like this:
| Field in MongoDB | Value (doc 1) | Plain English |
|---|---|---|
item | "ABC1" | Product code |
colors | ["blue","black","red"] | Array of color names |
How It Works
- Insert once. Five documents land in
inventory. - Mixed shapes. Arrays, empty array, missing field, and a string—useful for safe
$size. - find() shows the same shape. Ready for counting next.

