Im trying to use mongoose paginate to paginate through an array of values.
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(Travel); TravelSchema.plugin(mongoosePaginate); So subnotes are updated weekly and will have lots of information that i would like to paginate on service sice. For that im providing apage and limit like arguments in my controllers
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,
);
}
}
And on the service im injecting my document as a Paginate Model and trying to implement the service like this:
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);
}
Yet the paginate isnt doing anything, the entire country data gets selected. Any tip?
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);
}
So limit is basically duplicating whats inside my subnotes. If i put Limit 1, returns everything aka 200 documents. If i put Limit 2, returns 400 documents. :|