Tutorials Logic, IN info@tutorialslogic.com

MongoDB Relationships One to Many $lookup: Tutorial, Examples, FAQs & Interview Tips

MongoDB Relationships One to Many $lookup

MongoDB in MongoDB is best learned by connecting the rule to a product catalog or user activity store. Start with the smallest collection query, observe the output, and then add one realistic constraint so the concept becomes practical.

The key habit for this lesson is to watch document shape and index as it changes. That makes the topic easier to debug, easier to explain in interviews, and easier to use in real code without memorizing isolated syntax.

Modelling Relationships in MongoDB

MongoDB supports two primary strategies for representing relationships: embedding (denormalization) and referencing (normalization). Unlike SQL, there are no foreign key constraints - you manage relationships in application logic or through the aggregation pipeline.

Embedded Documents

One-to-One and One-to-Many Embedded

One-to-One and One-to-Many Embedded
// ONE-TO-ONE EMBEDDED: User with address
{
  "_id": ObjectId("u1"),
  "name": "Alice Johnson",
  "address": { "street": "123 Main St", "city": "New York", "zip": "10001" }
}

// ONE-TO-MANY EMBEDDED: Post with comments (bounded array)
{
  "_id": ObjectId("p1"),
  "title": "Getting Started with MongoDB",
  "comments": [
    { "user": "Bob",   "text": "Great article!", "date": ISODate("2024-01-10") },
    { "user": "Carol", "text": "Very helpful.",   "date": ISODate("2024-01-11") }
  ]
}

// Query embedded field
db.posts.find({ "comments.user": "Bob" })

// Update embedded array element using positional operator
db.posts.updateOne(
  { _id: ObjectId("p1"), "comments.user": "Bob" },
  { $set: { "comments.$.text": "Updated comment!" } }
)

Manual References

One-to-Many Referenced with $lookup

One-to-Many Referenced with $lookup
// users collection
{ "_id": ObjectId("u1"), "name": "Alice Johnson", "email": "alice@example.com" }

// orders collection - each order references the user
{ "_id": ObjectId("o1"), "userId": ObjectId("u1"), "total": 1299.99, "status": "shipped" }
{ "_id": ObjectId("o2"), "userId": ObjectId("u1"), "total": 45.00,   "status": "pending" }

// Join using $lookup aggregation
db.users.aggregate([
  { $match: { _id: ObjectId("u1") } },
  { $lookup: {
      from: "orders",
      localField: "_id",
      foreignField: "userId",
      as: "orders"
  }},
  { $project: { name: 1, email: 1, orderCount: { $size: "$orders" }, orders: 1 } }
])

Many-to-Many Relationships

Many-to-Many with Arrays of References

Many-to-Many with Arrays of References
// Students and Courses - many-to-many
// students collection
{
  "_id": ObjectId("s1"),
  "name": "Bob Smith",
  "enrolledCourses": [ObjectId("c1"), ObjectId("c2")]
}

// courses collection
{
  "_id": ObjectId("c1"),
  "title": "MongoDB Fundamentals",
  "enrolledStudents": [ObjectId("s1"), ObjectId("s2")]
}

// Find all courses a student is enrolled in
db.courses.find({ _id: { $in: [ObjectId("c1"), ObjectId("c2")] } })

// Add a student to a course
db.courses.updateOne(
  { _id: ObjectId("c1") },
  { $addToSet: { enrolledStudents: ObjectId("s3") } }
)
db.students.updateOne(
  { _id: ObjectId("s3") },
  { $addToSet: { enrolledCourses: ObjectId("c1") } }
)

Embed vs Reference Decision Guide

Embed vs Reference Decision Guide
// EMBED when:
// - Data is always accessed together with the parent
// - The sub-document is small and bounded (e.g., max 10-20 items)
// - The sub-document is not shared across multiple parents
// - You want atomic reads/writes in a single operation

// REFERENCE when:
// - Data is accessed independently of the parent
// - The array could grow unboundedly (e.g., all comments on a viral post)
// - The same data is referenced by multiple documents
// - The sub-document is large and rarely needed

// Example: User profile - EMBED (always needed together)
{ "_id": ObjectId("u1"), "name": "Alice", "profile": { "bio": "...", "avatar": "..." } }

// Example: User orders - REFERENCE (many orders, accessed separately)
// orders: { userId: ObjectId("u1"), total: 99.99, ... }
// db.orders.find({ userId: ObjectId("u1") })

Applied guide for MongoDB

Use MongoDB when the program needs a clear answer to a specific problem, not because the keyword looks familiar. In a real MongoDB task, first name the input, then name the transformation, then name the output. This small discipline shows whether the topic is being used correctly or only copied from an example.

A reliable practice flow is: create the smallest working collection query, add one normal case, add one edge case such as missing, repeated, empty, or boundary input, and then confirm the result with explain plan and sample documents. If the result surprises you, reduce the code until the behavior is visible again.

The most common trap here is copying the syntax before understanding the behavior. Avoid it by writing one sentence before the code that explains why MongoDB is the right choice. After the code runs, verify the lesson by doing this: change one input and explain the changed output.

  • Identify the exact problem solved by MongoDB.
  • Trace document shape and index before and after the main operation.
  • Keep one intentionally broken version and explain the fix.
  • Connect the example to a product catalog or user activity store so the idea feels concrete.
Key Takeaways
  • I can explain where MongoDB fits inside a product catalog or user activity store.
  • I can point to the exact document shape and index affected by this topic.
  • I tested a normal case and an edge case involving missing, repeated, empty, or boundary input.
  • I verified the result with explain plan and sample documents instead of assuming it worked.
  • I can describe the main mistake: copying the syntax before understanding the behavior.
Common Mistakes to Avoid
WRONG Copying the syntax before understanding the behavior.
RIGHT Write the expected behavior first, then make the example prove it.
A one-line expectation turns the code from copied syntax into a testable idea.
WRONG Practicing only the perfect input.
RIGHT Also test missing, repeated, empty, or boundary input before considering the lesson complete.
The edge case is where most interview follow-up questions begin.
WRONG Looking only at the final output.
RIGHT Trace document shape and index through each important step.
Tracing makes debugging faster because you can see the first incorrect state.

Practice Tasks

  • Build one small collection query that demonstrates MongoDB in a product catalog or user activity store.
  • Change the example to include missing, repeated, empty, or boundary input and record the difference.
  • Break the example by deliberately copying the syntax before understanding the behavior, then write the corrected version.
  • Explain the finished example in five bullet points: input, operation, output, failure case, and verification.

Frequently Asked Questions

Use it when the problem matches the behavior shown in the example and when the result can be verified through explain plan and sample documents.

Start with a tiny case, then test missing, repeated, empty, or boundary input. The main warning sign is copying the syntax before understanding the behavior.

Trace document shape and index, predict the result, run the example, and compare your prediction with the actual output.

Ready to Level Up Your Skills?

Explore 500+ free tutorials across 20+ languages and frameworks.