Mongodb Nested populate with keys of array type

24 Views Asked by At

i need to populate teamPlayes array which is having id's of users which is store in key teamScore which is also store in score key scores[0]teamScores[0]teamPlayers this is mongo schema

`

const matchSchema = mongoose.Schema(
     {
     scores: [
      {
        roundNumber: {
          type: Number,
          required: true,
        },
        teamScores: [
          {
            team: {
              type: Number,
              required: true,
            },
            teamPlayers: {
              type: [
                {
                  type: mongoose.Schema.Types.ObjectId,
                  ref: "User",
                },
              ],
              default: [],
            },
            score: {
              type: Number,
              default: 0,
            },
          },
        ],
      },
      ],
     },
     {
      timestamps: { createdAt: true, updatedAt: true },
     }
     );

`

this is what i have tries option 1:

const match = await matchModel
     .find()
     .populate({
       path: "scores.$.teamScores.$.teamPlayers",
       select: "firstName lastName",
     })
     .skip(pagination(page, limit))
     .limit(limit ?? 20);

option 2:

const match = await matchModel
     .find()
     .populate({
      path: "scores.teamScores.teamPlayers",
       select: "firstName lastName",
     })
     .skip(pagination(page, limit))
     .limit(limit ?? 20);

option 3:

const match = await matchModel
      .find()
      .populate({
      path: "scores.teamScores",
      select: "teamPlayers",
      populate:{path: "teamPlayers",
      select: "firstName lastName",
      model: 'User'
      }
      })
      .skip(pagination(page, limit))
      .limit(limit ?? 20);

this are all changes that i have tried

0

There are 0 best solutions below