我尝试使用 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 个文档。 :|