Pagination with mongoose and nestjs

早过忘川 提交于 2021-01-29 08:08:01

问题


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. :|


回答1:


You can implement this using the mongoose-paginate-plugin.

A possible starting point;

import { Inject, Injectable } from '@nestjs/common';
import {
  Connection, Document, PaginateModel, Schema,
} from 'mongoose';
import { InjectConnection } from '@nestjs/mongoose';

import TravelSchema from './travel.schema';

interface IRunAnything extends Document{
  [key: string]: any;
}

type SampleModel<T extends Document> = PaginateModel<T>;

@Injectable()
export default class ProfileRepository {
  constructor(
    @InjectConnection() private connection: Connection
  ) {}

  /**
   * Run the query.
   * The query are just your filters.
   *
   * @param query
   * @param offset
   * @param limit
   */
  async getTravelInfo(query: object, offset = 10, limit = 10) {

    const dynamicModel: SampleModel<IRunAnything> = this.connection.model<IRunAnything>('Travel', this.schema) as SampleModel<IRunAnything>;

    const customLabels = {
      docs: 'nodes',
      page: 'currentPage',
      totalPages: 'pageCount',
      limit: 'perPage',
      totalDocs: 'itemCount',
    };

    return dynamicModel.paginate(query, { customLabels, offset, limit });
  }
}

The solution is a bit hackish but you can build on it and adapt to your use case.

Remember to add the plugin to your schema.

npm i mongoose-paginate-v2

...
import * as mongoosePaginate from 'mongoose-paginate-v2';

TravelSchema.plugin(mongoosePaginate)

...


来源:https://stackoverflow.com/questions/64889678/pagination-with-mongoose-and-nestjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!