Example 1 — Sample Data
Imagine three shopping lists. Each has a name and an items array. We save them in a collection named lists.
db.lists.insertMany([
{ _id: 1, name: "grocery", items: ["milk", "eggs", "bread", "eggs"] },
{ _id: 2, name: "party", items: ["chips", "soda", "cups"] },
{ _id: 3, name: "bakery", items: ["bread", "butter"] }
]); How the Data Is Stored in MongoDB
After the insert, MongoDB keeps three documents in lists. Run find() to see them:
db.lists.find() Read those documents like this:
| Field in MongoDB | Example | Plain English |
|---|---|---|
_id | 1 | List id for this lesson |
name | "grocery" | List label |
items | ["milk", "eggs", "bread", "eggs"] | Ordered shopping items |
grocery items:
index: 0 1 2 3
["milk", "eggs", "bread", "eggs"]
first "eggs" → index 1
"butter" → not here (-1)
"eggs" from 2 → index 3 (second eggs) How It Works
- Insert once.
insertManyadds three list documents. - MongoDB stores arrays as written. Order in
itemsdecides the indexes. - find() shows the same shape. What you inserted is what you get back—ready for
$indexOfArraynext.

