Example 1 — Sample Data
Imagine one notebook with attributes stored two ways: as { k, v } documents and as [key, value] arrays. We save both in a collection named products.
db.products.insertOne({
_id: 1,
item: "Notebook",
pairs: [
{ k: "color", v: "blue" },
{ k: "pages", v: 200 }
],
tuples: [
["color", "blue"],
["pages", 200]
]
}); How the Data Is Stored in MongoDB
After the insert, MongoDB keeps one document in products. Run find() to see it:
db.products.find() Read that document like this:
| Field in MongoDB | Value | Plain English |
|---|---|---|
_id | 1 | Document id (row number for this lesson) |
item | "Notebook" | Product name |
pairs | [{k,v}, ...] | Attributes as key/value documents |
tuples | [[key, value], ...] | Same attributes as two-element arrays |
Pairs list -> one object:
{ k: "color", v: "blue" }
{ k: "pages", v: 200 }
|
v
{ color: "blue", pages: 200 }
Same idea with tuples:
["color", "blue"], ["pages", 200] -> same object How It Works
- Insert once.
insertOneadds a single product document toproducts. - Two formats, same meaning.
pairsandtuplesboth describe color and pages. - find() shows the same shape. What you inserted is ready for
$arrayToObjectnext.

