Example 1 — Sample Data
Imagine two students. Asha learned HTML/CSS then CSS/JS. Ben learned Python then Python/SQL. We save both in a collection named students.
db.students.insertMany([
{
_id: 1,
name: "Asha",
courseA: ["html", "css"],
courseB: ["css", "js"]
},
{
_id: 2,
name: "Ben",
courseA: ["python"],
courseB: ["python", "sql"]
}
]); How the Data Is Stored in MongoDB
After the insert, MongoDB keeps two documents in students. Run find() to see them:
db.students.find() Read those documents like this:
| Field in MongoDB | Example (Asha) | Plain English |
|---|---|---|
_id | 1 | Document id |
name | "Asha" | Student name |
courseA | ["html", "css"] | Skills from the first course |
courseB | ["css", "js"] | Skills from the second course |
Asha:
courseA ∪ courseB
{html, css} ∪ {css, js} → {html, css, js}
Ben:
courseA ∪ courseB
{python} ∪ {python, sql} → {python, sql} How It Works
- Insert once.
insertManyadds two student documents tostudents. - Two skill arrays. Each course contributes its own list; overlap (like
css) is expected. - find() shows both rows. What you inserted is what you get back—ready for the union next.

