首页 > 解决方案 > NestJS 填充仅填充第一个元素

问题描述

我在 NestJS 中有一个 Mongoose 模式,如下所示:


...
@Prop()
  casinoAmount: number;

  @Prop()
  gameHyperLink: string;

  @Prop()
  casinoHyperLink: string;

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

我想进行一个查询,这会产生一个完整的“左连接”。基本上是一个Provider从相应游戏中选择所有元素的查询。

如果我进行常规查询(没有populate),它将简单地返回所有ObjectID's

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

示例有效载荷

但是,当我像这样填充

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

游戏负载

但是,它只返回一个game对象,而不是所有对象的列表。

有人知道为什么会这样吗?

标签: node.jstypescriptexpressmongoosenestjs

解决方案


首先,exec()当您使用 await 进行查询和使用时不要使用,skip()并且limit()像这样作为填充中的选项:

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

    })

推荐阅读