Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

The best method of relations between two collection

I am trying to create a parent-child relationship between two collections in MongoDB.

The first way:

// users document
{
  "_id": "63e422adcb1ee8e6ffa4f4c7",
  "name": "Joe"
}

// address documents
{
  "_id": "77775554441ee8e6ffa4f4c7",
   parent_id: "63e422adcb1ee8e6ffa4f4c7", // reference to users document
   street: "123 Fake Street",
   city: "Faketon",
   state: "MA",
   zip: "12345"
},
{
  "_id": "55545adcb1ee8e6ffa4f4c7a",
   parent_id: "63e422adcb1ee8e6ffa4f4c7", // reference to users document
   street: "123 Fake Street",
   city: "Faketon",
   state: "MA",
   zip: "12345"
}

Here have the users collection will have a unique "_id" objectid, and the address collection will have a "_id" objectid and a "parent_id" field that references the parent "_id".

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel


The second way

// users document
{
  "_id": "63e422adcb1ee8e6ffa4f4c7",
  "name": "Joe",
  [
    "77775554441ee8e6ffa4f4c7", // reference to address document
    "55545adcb1ee8e6ffa4f4c7a"  // reference to address document
  ]
}

// address documents
{
  "_id": "77775554441ee8e6ffa4f4c7",
   street: "123 Fake Street",
   city: "Faketon",
   state: "MA",
   zip: "12345"
},
{
  "_id": "55545adcb1ee8e6ffa4f4c7a",
   street: "123 Fake Street",
   city: "Faketon",
   state: "MA",
   zip: "12345"
}

Here, the first method mirrored the users collection the _id of the address collection

Which is better for performance, This is not a discussion, but I really have a problem that the child collection contains more than 10 million and I was thinking of distributing the data to the parent.

>Solution :

Assuming that the parent_id field and the unnamed child array are indexed, the performance of reads will be about the same for both.

The difference in writes would be while addresses the first way permits just inserting the new addresses, while the second way would also require updating the user document.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading