Example 1 — Users and orders collections
db.users.drop()
db.orders.drop()
db.users.insertMany([
{ _id: 1, name: "dave123", tier: "gold", active: true },
{ _id: 2, name: "dave2", tier: "silver", active: true },
{ _id: 3, name: "ahn", tier: "gold", active: true },
{ _id: 4, name: "li", tier: "bronze", active: false },
{ _id: 5, name: "annT", tier: "silver", active: true },
{ _id: 6, name: "li", tier: "gold", active: true },
{ _id: 7, name: "ty", tier: "bronze", active: true }
])
db.orders.insertMany([
{ product: "Widget", amount: 99, status: "shipped", region: "North" },
{ product: "Gadget", amount: 149, status: "shipped", region: "South" },
{ product: "Gizmo", amount: 49, status: "pending", region: "East" },
{ product: "Widget", amount: 99, status: "shipped", region: "West" },
{ product: "Tool", amount: 199, status: "cancelled", region: "North" },
{ product: "Part", amount: 25, status: "shipped", region: "South" },
{ product: "Kit", amount: 75, status: "shipped", region: "East" },
{ product: "Pack", amount: 55, status: "pending", region: "West" }
]) How It Works
Seven users and eight orders—enough to demonstrate random selection, filtered pools, and the difference between $sample and $limit.

