Referencing stores a pointer (typically an ObjectId) to a document in another collection, mirroring the foreign-key pattern in relational databases.
{
_id: ObjectId("order1"),
customerId: ObjectId("cust1"),
status: "completed",
total: 149.99
}
{ _id: ObjectId("cust1"), name: "Alice", email: "alice@example.com" }
const order = db.orders.findOne({ _id: orderId })
const customer = db.customers.findOne({ _id: order.customerId })
{ _id: ObjectId("post1"), title: "MongoDB Tips", body: "..." }
{ _id: ObjectId("c1"), postId: ObjectId("post1"), user: "Bob", text: "Great!" }
{ _id: ObjectId("c2"), postId: ObjectId("post1"), user: "Carol", text: "Agreed!" }
db.comments.find({ postId: ObjectId("post1") })
db.comments.createIndex({ postId: 1 })
Strengths of Referencing
- Unbounded growth: child collection can grow to any size without affecting parent document
- Single source of truth: one document to update when shared data changes
- Independent queries: child data can be queried, paginated, and indexed on its own
- Smaller parent documents: parent stays lean — less RAM to load for common parent-only reads
WARN
Cross-document updates are not atomic by default. If your write must update two documents together (e.g., decrement inventory AND create order), you need multi-document transactions (MongoDB 4.0+), which add overhead. Embedding eliminates this need by placing both pieces of data in one document.