使用 mongoose 和 Nestjs 进行分页

2024-01-09

我尝试使用 mongoose paginate 对一组值进行分页。

class subNotes {
  @Prop()
  Note: string;
  @Prop()
  Date: Date;
}
@Schema()
class Travel extends Document {
  @Prop()
  Country: string;
  @Prop()
  Recommendation: string;
  @Prop()
  Level: number;
  @Prop()
  LevelDescr: string;
  @Prop()
  Date: Date;
  @Prop()
  Notes: [subNotes];
}

导出 const TravelSchema = SchemaFactory.createForClass(Travel); TravelSchema.plugin(mongoosePaginate); 因此,子注释每周更新一次,并且将包含大量我想在服务页面上分页的信息。 为此,我在控制器中提供了类似参数的页面和限制

async createSlugs(
    @Query('page') page = 1,
    @Query('limit') limit = 10,
    @Param('country') country: string,
  ) {
    limit = limit > 100 ? 100 : limit;
    return await this.travelService.getTravelInfo(
      {
        limit: Number(limit),
        page: Number(page),
      },
      country,
    );
  }
}

在服务上,我将我的文档作为分页模型注入,并尝试像这样实现该服务:

 async getTravelInfo(page = 1, limit = 10, country: string) {
    await this.saveTravelInfo(country);

    const options = {
      page: Number(page),
      limit: Number(limit),
    };

    return await this.notesModel.paginate({ Country: country }, options);
  }

然而分页没有做任何事情,整个国家/地区的数据都被选中。有什么建议吗?

async getTravelInfo(page = 1, limit = 10, country: string) {
    await this.saveTravelInfo(country);

    const options = {
      populate: 'subNotes',
      page: Number(page),
      limit: Number(limit),
    };
    console.log(options);

    return await this.notesModel.paginate({ Country: country }, options);
  }

所以限制基本上是复制我的子注释中的内容。如果我设置限制 1,则返回所有内容,即 200 个文档。如果我设置限制 2,则返回 400 个文档。 :|


所以看看我做了这个包 使用 mongoose 使 Nestjs 中键集分页的实现变得简单 ????

https://www.npmjs.com/package/nestjs-keyset-paginator https://www.npmjs.com/package/nestjs-keyset-paginator

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 mongoose 和 Nestjs 进行分页 的相关文章

随机推荐