aravindnc / mongoose-aggregate-paginate-v2

A cursor based custom aggregate pagination library for Mongoose with customizable labels.
MIT License
131 stars 23 forks source link

not working with mongoDb cluster 7 #60

Open Bamorem opened 2 months ago

Bamorem commented 2 months ago

After upgrating my cluster 6 to 7

my aggregatePaginate() doesn't woke using $geoNear.

It was working under a cluster 6, but not 7.

And the same aggregate is working with a simple aggregate() without paginate using geoNear.

saminndex commented 2 months ago

Same here $geoNear is not allowed to be used within a $facet stage

saminndex commented 2 months ago

I've fixed this @Bamorem - the documentation has this:

[useFacet] {Bool} - To use facet operator instead of using two queries. This is the new default. (Default: true)

So if you just change this to false it will resolve the issue

erksdee commented 1 month ago

I have also set the useFacet option to false, but it seems like the pagination is not working correctly. The nextPage is returning false, which is incorrect since I know there is more data. If I set it back to true, the pagination works correctly, but I get the error $_internalSearchMongotRemote is not allowed to be used within a $facet stage when I add the $search.

Can someone please advise? I'm not sure what I'm doing wrong.

Here is my pipeline:

const pipeline = [
  {
    $search: {
      index: 'title',
      text: {
        query: 'xxxxxxx',
        path: {
          wildcard: '*',
        },
      },
    },
  },
  {
    $match: {
      status: 'active',
      tags: {
        $in: ['tag1', 'tag2'],
      },
    },
  },
];

FYI, the above pipeline works with "mongoose-aggregate-paginate-v2": "1.0.7". But not with "mongoose-aggregate-paginate-v2": "1.1.1".

MattLicense commented 1 month ago

@erksdee I found the solution to this to be passing in your aggregate as the countQuery in the paginationOptions. While debugging the issue I noticed once we set useFacet: false because we were also using $search in our pipeline.

So where before we had this:

      const aggregation = this.aggregateModel.aggregate(
        stages,
        { session: session },
      );

      return await this.aggregateModel.aggregatePaginate(aggregation, paginationOptions);

where paginationOptions contained useFacet: false, we now have this:

      const aggregation = this.aggregateModel.aggregate(
        stages,
        { session: session },
      );

      return await this.aggregateModel.aggregatePaginate(
        aggregation,
        {
          ...paginationOptions,
          countQuery: aggregation,
        },
      );

The issue appears to be here. constructPipeline correctly creates the query to count the total results, but it's not used when useFacet is false. If no countQuery is provided then the variable pipeline is used which is the limited query used to get a page of results, rather than the full results.

aravindnc commented 1 month ago

@MattLicense Would be nice if you can do a PR.