mongoose-aggregate-paginate-v2
mongoose-aggregate-paginate-v2 copied to clipboard
I can't use mongoose-aggregate-paginate-v2 and mongoose-paginate-v2 in the same project
in a part of the project I need to use mongoose-paginate-v2 and it works perfect
the problem occurs when I try to implement mongoose-aggregate-paginate-v2 in another data model
because when I install it it warns me that in the implementation of the first library (mongoose-paginate-v2) there is no "paginate ()" function
Is there a way i can use both plugins?
Is the issue happening with TS version ?
Yes, TS version
Hermano, encontre la solucion, no es tanto una solucion potente, pero es lo que hay, toca que lo importes directamente asi bro, con el [ var aggregatePaginate = require("mongoose-aggregate-paginate-v2"); ] y ya te va a jalar.
@ErickCincoAyon English please!
Brother, I found the solution, it is not so much a powerful solution, but it is what it is, just import it directly like this bro, with [ var addedPaginate = require("mongoose-aggregate-paginate-v2"); ] and he's going to pull you.
And here is one query:
@ErickCincoAyon Appreciate it. Thank you.
I forgot add this, if u want use the typed of result queries, you should add this
into
This comment could be a bit offtopic, please someone, tell me if i should open another issue or idk, smth like that.
@ErickCincoAyon I think you're using NestJS, correct me if i'm wrong. If you're using it, how did you're injecting the model? I injected it with the next code:
@InjectModel(PaymentsRequest.name)
private readonly paymentsRequestModel: AggregatePaginateModel<PaymentsRequest>
and i'm importing the plugin like you, with require, then calling Schema.plugin(aggregatePaginate)
inside the entity file, but when i'm trying to call .aggregatePaginate
, like this:
const aggregateQuery = this.paymentsRequestModel.aggregate([
// query...
]);
return await this.paymentsRequestModel.aggregatePaginate(aggregateQuery, paginationData);
it throws me an error that says that .aggregatePaginate
is undefined, i installed @types/mongoose-aggregate-paginate-v2
and mongoose-aggregate-paginate-v2
with yarn
, but seems that it's not working for some reason, or well, looks like the plugin isn't right applied, and in fact, i don't really know why aggregatePaginate
seems to be undefined, when i'm applying plugin
to the Schema. I tried too using .plugin
inside the module, like this:
import { Module } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
import { MyService } from './my.service';
import { MyController } from './my.controller';
import { PaymentsRequest, PaymentsRequestSchema } from '../payments-requests/entities/payments-request.entity';
const mongooseAggregatePaginate = require('mongoose-aggregate-paginate-v2');
@Module({
providers: [MyService],
controllers: [MyController],
imports: [
MongooseModule.forFeatureAsync([
{
name: PaymentsRequest.name,
useFactory: () => {
const schema = PaymentsRequestSchema
schema.plugin(mongooseAggregatePaginate)
return schema
}
}
])
]
})
but having the same problem :/
If anyone know what can i'm doing bad, please tell me anyways
@AlphaTechnolog Hi, I'm using nestjs also, this import for injected model works as expected:
import { AggregatePaginateModel } from 'mongoose';
and then in service class:
constructor(
@InjectModel(User.name) private userModel: AggregatePaginateModel<UserDocument>,
) {}
@AlphaTechnolog Hi, I'm using nestjs also, this import for injected model works as expected:
import { AggregatePaginateModel } from 'mongoose';
and then in service class:
constructor( @InjectModel(User.name) private userModel: AggregatePaginateModel<UserDocument>, ) {}
Oh, thank you Anyways, but i ended up implementing the pagination myself lol, I'll try with this the next time then haha
Is it possible to use both in the same model?
Yes you can, you only need to add the plugin in the model and other configs:
In your schema
import paginateWithAgregate from 'mongoose-aggregate-paginate-v2';
...
MyBookSchema.plugin(paginateWithAgregate);
then in your Model:
import mongoose, { Document, PaginateModel, AggregatePaginateModel } from "mongoose";
...
export interface MyBookModel extends PaginateModel<MyBookDocument>, AggregatePaginateModel<MyBookDocument> {
...
findBooksWith: (
this: AggregatePaginateModel<MyBookDocument>,
param1: string,
param2: number
) => Promise<Pagination>
}
and finally you can use it:
const pipeline = [{ $match: { author: "jhon doe" } }]
const myAggregate = MyBookModel.aggregate(pipeline);
const queryResults = await MyBookModel.aggregatePaginate(myAggregate, options);