aravindnc/mongoose-aggregate-paginate-v2

Being able to pass query directly/throw error if wrong usage

Closed this issue · 2 comments

Hi,

As i was trying to use the package, i was mislead into thinking you could pass a query object, instead of a mongose.model.aggregate() object.

The documention on npm states:
Model.aggregatePaginate([aggregateQuery], [options], [callback])
[aggregate-query] {Object} - Aggregate Query criteria.

So, i tried to call the aggregatePaginate method like so:

let test = Models.MarketOffer.aggregatePaginate(          [
      {$match: config},
       {$addFields: {item: {$arrayElemAt: ['$items', 0]}}},
      {$unset: 'items'},
      {
        $lookup: {
          from: 'items',
          localField: 'item._tpl',
          foreignField: '_id',
          as: 'infos'
        }
      },
      {
        $addFields: {
          info: { $arrayElemAt: ['$infos', 0] }
        }
      },
      { $unset: 'infos' },
      {
        $addFields: {
          name: '$info._props.Name',
          shortName: '$info._props.ShortName',
          description: '$info._props.Description'
        }
      },
      { $unset: 'info' }
    ], {page: params.page}
    );

which obviously didn't work.

One could add a test to the aggregateQuery object, testing if the following properties exist
_pipeline
_model
options

If the test isn't succesful, possibly throw an error

Not really a real issue, more like QOL improvement.

I could try to set-up the pull request if you feel like it could be useful :)

@devolution2409 If you can do a pull-request then that'd be great.

@devolution2409 Did your issue got resolved?