Example 1 — People, students, and contacts
db.people.drop()
db.students.drop()
db.contacts.drop()
db.people.insertMany([
{ _id: 1, name: "Arlene", age: 34, pets: { dogs: 2, cats: 1 } },
{ _id: 2, name: "Sam", age: 41, pets: { cats: 1, fish: 3 } },
{ _id: 3, name: "Maria", age: 25 }
])
db.students.insertMany([
{
_id: 1,
studentName: "Alex",
grades: [
{ test: 1, score: 80, subject: "Math" },
{ test: 2, score: 92, subject: "Math" },
{ test: 3, score: 88, subject: "Science" }
]
},
{
_id: 2,
studentName: "Jordan",
grades: [
{ test: 1, score: 75, subject: "Math" },
{ test: 2, score: 95, subject: "Science" }
]
}
])
db.contacts.insertMany([
{ _id: 1, first_name: "Gary", last_name: "Sheffield", city: "New York" },
{ _id: 2, first_name: "Nancy", last_name: "Walker", city: "Anaheim" },
{ _id: 3, first_name: "Peter", last_name: "Sumner", city: "Toledo" }
]) How It Works
Three collections: embedded pets objects, grades arrays, and flat contact name fields—covering promote, unwind, and create-new patterns.

