Example 1 — Sample Data
Imagine four orders. Each has a store, a city, and a tags array. Notice store A has New York twice—perfect for showing how $addToSet drops duplicates.
db.orders.insertMany([
{ store: "A", city: "New York", tags: ["sale", "new"] },
{ store: "A", city: "New York", tags: ["sale"] },
{ store: "A", city: "Chicago", tags: ["clearance"] },
{ store: "B", city: "New York", tags: ["new"] }
]); How the Data Is Stored in MongoDB
After the insert, MongoDB keeps 4 documents in orders. Run find() to see them:
db.orders.find() Read each document like this:
| Field in MongoDB | Meaning | Example |
|---|---|---|
store | Which store made the order | "A" |
city | City for that order | "New York" |
tags | Labels on the order (array) | ["sale","new"] |
Orders by store:
Store A: New York, New York, Chicago
Store B: New York
Notice Store A has "New York" twice —
$addToSet will keep it only once. How It Works
- Insert once. The sample command adds 4 documents to
orders. - MongoDB stores fields as written. Each document keeps the same field names and values you inserted.
- find() shows the same shape. What you inserted is what you get back—ready for
$addToSetin the next examples.

