Example 1 — Sample Data
Imagine four users. Each has a favorites array of different lengths.
db.users.insertMany([
{ _id: 1, name: "dave123", favorites: ["chocolate", "cake", "butter", "pie", "nuts"] },
{ _id: 2, name: "li", favorites: ["apples", "pudding", "pie"] },
{ _id: 3, name: "ahn", favorites: ["pears", "pecans", "chocolate", "berries"] },
{ _id: 4, name: "ty", favorites: ["ice cream"] }
]); How the Data Is Stored in MongoDB
After the insert, MongoDB keeps four documents. Run find() to see them:
db.users.find() Read one document like this:
| Field in MongoDB | Value (doc 1) | Plain English |
|---|---|---|
name | "dave123" | Username |
favorites | ["chocolate", …] | Array of favorite foods |
How It Works
- Insert once. Four documents land in
users. - Different lengths. Long lists and a one-item list show how
$slicebehaves when short. - find() shows the same shape. Ready for slicing next.

