Example 1 — Sample Data
Imagine three users. Each has a name and a favorites list. We save them in a collection named users.
db.users.insertMany([
{ _id: 1, name: "dave123", favorites: ["chocolate", "cake", "apples"] },
{ _id: 2, name: "li", favorites: ["apples", "pie"] },
{ _id: 3, name: "ahn", favorites: ["pears", "pie", "cherries"] }
]); How the Data Is Stored in MongoDB
After the insert, MongoDB keeps three documents in users. Run find() to see them:
db.users.find() Read those documents like this:
| Field in MongoDB | Example | Plain English |
|---|---|---|
_id | 1 | User id for this lesson |
name | "dave123" | User name |
favorites | ["chocolate", "cake", "apples"] | Ordered list of favorite items |
dave123 favorites:
index: 0 1 2
["chocolate", "cake", "apples"]
end: -3 -2 -1
first (0) → "chocolate"
last (-1) → "apples"
second (1) → "cake" How It Works
- Insert once.
insertManyadds three user documents. - MongoDB stores arrays as written. Order in
favoritesmatters for indexes. - find() shows the same shape. What you inserted is what you get back—ready for
$arrayElemAtnext.

