6

I have a collection(matches) of data with one virtual property referencing another collection(rosters). Collection(rosters) has a reference to another virtual property in collection(participants). I have successfully populated collection(rosters). However, I am unable to populate collection(participants).

Matches

{
"_id": {
    "$oid": "5c1b99843a71d812af25b10d"
},
"type": "match",
"id": "0b0ad010-ee86-11e8-b29a-061fc5dc976c",
"relationships": {
    "rosters": {
        "data": [
            {
                "type": "roster",
                "id": "8dc6f770-ee88-11e8-b755-9d41e8e54ecc"
            },
            {
                "type": "roster",
                "id": "8dc6f774-ee88-11e8-b755-9d41e8e54ecc"
            }
        ]
    }

}

}

Rosters

 "_id": {
    "$oid": "5c1b99843a71d812af25b151"
},
"type": "roster",
"id": "f51f1250-ee87-11e8-a185-711497405867",
"relationships": {
    "participants": {
        "data": [
            {
                "type": "participant",
                "id": "f51f1251-ee87-11e8-a185-711497405867"
            },
            {
                "type": "participant",
                "id": "f51f1252-ee87-11e8-a185-711497405867"
            },
            {
                "type": "participant",
                "id": "f51f1253-ee87-11e8-a185-711497405867"
            }
        ]
    }
}

Here is what I have so far.

Match.find([])
  .populate({
    path: 'rosters',  // virtual
    populate: {
      path: 'participant', // virtual
      model: 'Participant'
    }
  }

I expect to have a populated participant instance within my match instance, however the actual output is: participants: { data: [Array] }.

2 Answers2

0

I cannot test this right now, but have you either tried adding another populate call after the first one or tried a nested populate call like this: https://stackoverflow.com/a/36659971/2856218?

BenSower
  • 1,532
  • 1
  • 13
  • 26
0

you can do it like this: columns is the virtual of Board. tasks is the virtual of Column.

const board = await Board.find({ _id: boardId }).populate({
    path: "columns",
    populate: { path: "tasks" },
  });

you can read more about population in the documnet: https://mongoosejs.com/docs/populate.html#deep-populate