用mongoose和nestjs分页

用mongoose和nestjs分页,mongoose,pagination,nestjs,Mongoose,Pagination,Nestjs,我试图使用mongoose分页来通过一个值数组进行分页 class subNotes { @Prop() Note: string; @Prop() Date: Date; } @Schema() class Travel extends Document { @Prop() Country: string; @Prop() Recommendation: string; @Prop() Level: number; @Prop() LevelDe

我试图使用mongoose分页来通过一个值数组进行分页

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];
}
export const TravelSchema=SchemaFactory.createForClass(旅行); 插件(mongoosePaginate); 因此,子网每周更新一次,将有很多信息,我想在服务sice上分页。 为此,我在控制器中提供了类似于apage和limit的参数

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);
  }
然而paginate没有做任何事情,整个国家的数据都被选中了。有小费吗

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,则返回所有文档。如果我设置限制2,则返回400个文档:|

您可以使用

一个可能的起点

从'@nestjs/common'导入{Inject,Injectable};
进口{
连接、文档、分页模型、架构、,
}来自“猫鼬”;
从'@nestjs/mongoose'导入{InjectConnection};
从“/travel.schema”导入TravelSchema;
扩展文档的接口{
[键:字符串]:任意;
}
类型SampleModel=分页模型;
@可注射()
导出默认类档案库{
建造师(
@InjectConnection()专用连接:连接
) {}
/**
*运行查询。
*查询只是您的过滤器。
*
*@param查询
*@param偏移量
*@param限制
*/
异步getTravelInfo(查询:对象,偏移量=10,限制=10){
const dynamicModel:SampleModel=this.connection.model('Travel',this.schema)作为SampleModel;
常量自定义标签={
文档:“节点”,
页面:“当前页面”,
totalPages:'pageCount',
限制:'每页',
totalDocs:'itemCount',
};
返回dynamicModel.paginate(查询,{customLabels,offset,limit});
}

}
您可以使用

一个可能的起点

从'@nestjs/common'导入{Inject,Injectable};
进口{
连接、文档、分页模型、架构、,
}来自“猫鼬”;
从'@nestjs/mongoose'导入{InjectConnection};
从“/travel.schema”导入TravelSchema;
扩展文档的接口{
[键:字符串]:任意;
}
类型SampleModel=分页模型;
@可注射()
导出默认类档案库{
建造师(
@InjectConnection()专用连接:连接
) {}
/**
*运行查询。
*查询只是您的过滤器。
*
*@param查询
*@param偏移量
*@param限制
*/
异步getTravelInfo(查询:对象,偏移量=10,限制=10){
const dynamicModel:SampleModel=this.connection.model('Travel',this.schema)作为SampleModel;
常量自定义标签={
文档:“节点”,
页面:“当前页面”,
totalPages:'pageCount',
限制:'每页',
totalDocs:'itemCount',
};
返回dynamicModel.paginate(查询,{customLabels,offset,limit});
}
}