#1 India's Top IT Training Institute

×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
Embedded vs Referenced Data, One-to-Many, Many-to-Many

Data Modeling and Schema Design

Data modeling in MongoDB is fundamentally different from relational design. Instead of normalizing everything into separate tables, you decide — for every relationship — whether to embed related data or reference it in another collection.

1. Embedding vs Referencing

ApproachWhen to UseTrade-off
EmbeddingData is accessed together, doesn't grow unboundedFast reads, but can duplicate data and hit 16MB limit
ReferencingData is large, shared, or grows unboundedNormalized, but requires extra queries or $lookup

2. One-to-Few (Embedding)

{
  "_id": ObjectId("..."),
  "name": "Priya Mehta",
  "addresses": [
    { "type": "home", "city": "Jaipur" },
    { "type": "work", "city": "Gurgaon" }
  ]
}

3. One-to-Many (Referencing)

When the "many" side can grow large — like orders for a customer — reference by storing the parent's _id in the child document.

// customers collection
{ "_id": ObjectId("cust1"), "name": "Vikram Rao" }

// orders collection
{ "_id": ObjectId("ord1"), "customerId": ObjectId("cust1"), "total": 4599 }
{ "_id": ObjectId("ord2"), "customerId": ObjectId("cust1"), "total": 1299 }

4. Many-to-Many

Many-to-many relationships (e.g., students and courses) are typically modeled by referencing arrays of IDs on either or both sides.

// students collection
{ "_id": ObjectId("s1"), "name": "Ishaan", "courseIds": [ObjectId("c1"), ObjectId("c2")] }

// courses collection
{ "_id": ObjectId("c1"), "title": "Databases 101", "studentIds": [ObjectId("s1")] }
Design Principle: Model your schema around access patterns, not abstract entity relationships. Ask: "What data do I read together most often?" — then structure around that.

5. Hybrid Approach

Real-world schemas often mix both — embedding a summary while referencing the full record.

{
  "_id": ObjectId("ord3"),
  "customer": { "id": ObjectId("cust1"), "name": "Vikram Rao" }, // embedded summary
  "items": [ { "productId": ObjectId("p1"), "qty": 2 } ],        // referenced products
  "total": 3499
}

6. Data Modeling Checklist

  • ✅ Identify access patterns before designing the schema
  • ✅ Embed for one-to-few, tightly coupled data
  • ✅ Reference for one-to-many at scale and many-to-many
  • ✅ Watch out for unbounded array growth
  • ✅ Consider hybrid embedding + referencing where appropriate
Key Takeaway: There's no universally "correct" schema in MongoDB — the right design depends entirely on how your application reads and writes data. Model for your queries, not just your entities.

Ready to master MongoDB?

Build real-world MongoDB-powered applications with hands-on projects, mentor-led sessions, and placement support.

Explore Course