Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

shalu_104's avatar

Is it possible to create relationship with data in array in MongoDB

I have a collection with Data1 like

{
  _id : "1",
  total_amount : 5876,
  date: "20-Jun-2018"
},
{
  _id : "2",
  total_amount : 5776,
  date: "20-Jun-2018"
}

I want to store these data in another collection Data2 as follows :

{
   name : "XYZ",
   adjustments : [
   {
         data1_id : "1",
         adjust_amount : 675

   },
   {
         data1_id : "2",
         adjust_amount : 6785

   }

  ]
}

Storing this data isn't a problem in an array of Objects form. But is there any way I can create a relationship with the data within the array adjustments?

At the time of listing, I want adjustments with adjust_amount and all the related data.

0 likes
0 replies

Please or to participate in this conversation.