How would you model a one-to-many relationship in MongoDB

0 votes
Can you tell me How would you model a one-to-many relationship in MongoDB?
Feb 21 in Node-js by Ashutosh
• 20,870 points
47 views

1 answer to this question.

0 votes

In MongoDB, a one-to-many relationship can be modeled in multiple ways, depending on the use case and access patterns. The two most common approaches are embedding and referencing.

1. Embedding (Denormalization) - Best for Read-Heavy Operations

In this approach, related documents are stored within the parent document as an array.

Example: One author has many books (Embedded Approach)

{

  "_id": ObjectId("60f7c0b0c25f6d4d3c4b1e1f"),

  "name": "J.K. Rowling",

  "books": [

    { "title": "Harry Potter and the Sorcerer’s Stone", "year": 1997 },

    { "title": "Harry Potter and the Chamber of Secrets", "year": 1998 }

  ]

}

2. Referencing (Normalization) - Best for Write-Heavy & Large Collections

In this approach, related documents are stored separately, and the relationship is maintained using references (_id).

Example: Separate authors and books collections with references

// Authors Collection

{

  "_id": ObjectId("60f7c0b0c25f6d4d3c4b1e1f"),

  "name": "J.K. Rowling"

}


// Books Collection

{

  "_id": ObjectId("60f7c0b0c25f6d4d3c4b1e2a"),

  "title": "Harry Potter and the Sorcerer’s Stone",

  "year": 1997,

  "author_id": ObjectId("60f7c0b0c25f6d4d3c4b1e1f")

}

Query Example (Joining Data)

db.books.aggregate([

  {

    $lookup: {

      from: "authors",

      localField: "author_id",

      foreignField: "_id",

      as: "author_details"

    }

  }

])

answered Feb 22 by Kavya

Related Questions In Node-js

0 votes
1 answer

How do you model a many-to-many relationship in MongoDB with an example?

In MongoDB, a many-to-many relationship can be ...READ MORE

answered Feb 23 in Node-js by Kavya
67 views
0 votes
1 answer

How to check if a collection exists in Mongodb native nodejs driver?

Hello @kartik, The collectionNames method of the native driver's Db object accepts ...READ MORE

answered Nov 27, 2020 in Node-js by Niroj
• 82,840 points
14,500 views
0 votes
0 answers

How to train a model in nodejs (tensorflow.js)?

I'd like to create an image classifier, ...READ MORE

Aug 19, 2022 in Node-js by Neha
• 9,020 points
986 views
0 votes
1 answer

How can I implement user authentication with JWT in an Express.js app?

In an Express.js application, you can use ...READ MORE

answered Dec 17, 2024 in Java-Script by Navya
106 views
0 votes
1 answer

Is it possible to handle React events using the Chrome extension?

Yes, it's possible to handle React events ...READ MORE

answered Feb 22 in Node-js by Kavya
40 views
0 votes
1 answer

How can I use all the React events with Material-UI components?

The best approach is to leverage the ...READ MORE

answered Feb 22 in Node-js by Kavya
41 views
0 votes
1 answer

Why won't React events fire, or what could prevent them from firing?

If React events are not firing, several ...READ MORE

answered Feb 22 in Node-js by Kavya
46 views
0 votes
1 answer

How do you design a schema for tree structures in MongoDB?

Designing a schema for tree structures in ...READ MORE

answered Feb 22 in Node-js by Kavya
44 views
0 votes
1 answer

How do you embed a document in MongoDB for better performance?

Embedding documents in MongoDB is a common ...READ MORE

answered Feb 22 in Node-js by Kavya
66 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP