NestJS populate only populates first element

Viewed 570

I have a Mongoose schema in NestJS like such:


...
@Prop()
  casinoAmount: number;

  @Prop()
  gameHyperLink: string;

  @Prop()
  casinoHyperLink: string;

  @Prop({ type: Types.ObjectId, ref: 'Game' })
  games: Game[];
}

I want to make a query, that makes a full " left join". Basically a query that select each Provider with all elements from the corresponding games.

If i do a regular query (without populate) it will simple return all the ObjectID's

    const providers = await this.providerRepository
      .find({})
      .skip(offset)
      .limit(limit)
      .exec();

Example payload

However when i populate like such

    
const providers = await this.providerRepository
      .find({})
      .populate('games')
      .skip(offset)
      .limit(limit)
      .exec();

Payload with games

However, it only returns a single game object and not a list of all the objects.

Does anybody know why this occurs?

1 Answers

first of all don't use exec() when you are using await for query and use skip() and limit() like this as option in the populate:

await this.providerRepository.find({}).populate({
    path:'games',
    options: {
        limit: limit,
        skip: skip

    })
Related