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".


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.

Leave a Reply