Example 1 — Sample Data
Imagine one order draft with a cart and a wishlist. We save both arrays in a collection named shopping.
db.shopping.insertOne({
_id: 1,
shopper: "Asha",
cart: ["Notebook", "Pen"],
wishlist: ["Marker", "Eraser"]
}); How the Data Is Stored in MongoDB
After the insert, MongoDB keeps one document in shopping. Run find() to see it:
db.shopping.find() Read that document like this:
| Field in MongoDB | Value | Plain English |
|---|---|---|
_id | 1 | Document id (row number for this lesson) |
shopper | "Asha" | Who is shopping |
cart | ["Notebook", "Pen"] | Items already in the cart |
wishlist | ["Marker", "Eraser"] | Items still on the wishlist |
cart: [ Notebook, Pen ]
wishlist: [ Marker, Eraser ]
|
v
all_items:
[ Notebook, Pen, Marker, Eraser ] How It Works
- Insert once.
insertOneadds a single shopping document toshopping. - MongoDB stores arrays as written.
cartandwishlistkeep their order. - find() shows the same shape. What you inserted is ready for
$concatArraysnext.

