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

Aggregate not working when using paginate #19

Closed Sptandi closed 2 years ago

Sptandi commented 4 years ago

Hello, I make query using aggregate to filter by value in nested document like code show below :

  const trayekAggregate = await Trayek.aggregate([
    {
      $match: {
        $or: [
          {
            'track.destination.destinationName': {
              $regex: `${req.query.placeName}`,
              $options: 'i',
            },
          },
          {
            'track.destination.destinationAddress': {
              $regex: `${req.query.placeName}`,
              $options: 'i',
            },
          },
        ],
      },
    },
    {
      $project: {
        destination: '$track.destination.destinationName',
        destinationAddress: '$track.destination.destinationAddress',
      },
    },
  ]);

  Trayek.aggregatePaginate(trayekAggregate, options, (err, result) => {
    if (err) {
      console.log(err);
    } else {
      console.log(result);
    }
  });

Without paginete the result is something like this :

[
    {
        "_id": "5eb500356135626ec340497b",
        "destination": "St James Park, Daerah Istimewa Yogyakarta",
        "destinationAddress": "St James Park, Kabupaten Liverpool, Daerah Istimewa Yogyakarta"
    },
    {
        "_id": "5eb51472e142fc1175088e47",
        "destination": "Jogja City Mall, Daerah Istimewa Yogyakarta",
        "destinationAddress": "Jogja City Mall, Kabupaten Italy, Daerah Istimewa Yogyakarta"
    }
]

After use paginate, it's displaying all the data in the collection :

{
    "docs": [
        {
            "_id": "5eb500356135626ec340497b",
            "track": {
                "origin": {
                    "geoCode": [
                        -7.795579799999998,
                        110.369
                    ],
                    "originName": "Craven Cottage, Daerah Istimewa Yogyakarta",
                    "originAddress": "Craven Cottage, Kabupaten Italy, Daerah Istimewa Yogyakarta"
                },
                "destination": {
                    "geoCode": [
                        -7.795579799999998,
                        110.369123123123
                    ],
                    "destinationName": "St James Park, Daerah Istimewa Yogyakarta",
                    "destinationAddress": "St James Park, Kabupaten Liverpool, Daerah Istimewa Yogyakarta"
                },
                "type": "Point",
                "coordinates": [
                    {
                        "geoCode": [
                            -7.6709292,
                            109.6608012
                        ],
                        "_id": "5eb500356135626ec340497c",
                        "coordinateName": "St James Park"
                    },
                    {
                        "geoCode": [
                            -7.6709292,
                            109.6608012
                        ],
                        "_id": "5eb500356135626ec340497d",
                        "coordinateName": "London"
                    }
                ]
            },
            "bus": [
                "5eb5164169a23d148b409ea2"
            ],
            "trayekName": "Trayek Baru St James Park - London",
            "__v": 0
        },
        {
            "_id": "5eb51472e142fc1175088e47",
            "track": {
                "origin": {
                    "geoCode": [
                        -7.795579799999998,
                        110.369
                    ],
                    "originName": "Soli City Mall, Daerah Istimewa Yogyakarta",
                    "originAddress": "Soli City Mall, Kabupaten Italy, Daerah Istimewa Yogyakarta"
                },
                "destination": {
                    "geoCode": [
                        -7.795579799999998,
                        110.369123123123
                    ],
                    "destinationName": "Jogja City Mall, Daerah Istimewa Yogyakarta",
                    "destinationAddress": "Jogja City Mall, Kabupaten Italy, Daerah Istimewa Yogyakarta"
                },
                "type": "Point",
                "coordinates": [
                    {
                        "geoCode": [
                            -7.6709292,
                            109.6608012
                        ],
                        "_id": "5eb51472e142fc1175088e48",
                        "coordinateName": "Semarang"
                    },
                    {
                        "geoCode": [
                            -7.6709292,
                            109.6608012
                        ],
                        "_id": "5eb51472e142fc1175088e49",
                        "coordinateName": "Kebumen"
                    }
                ]
            },
            "bus": [],
            "trayekName": "Jogja-Kebumen-Semarang-Wq",
            "__v": 0
        }
    ],
    "totalDocs": 2,
    "limit": 10,
    "page": 1,
    "totalPages": 1,
    "pagingCounter": 1,
    "hasPrevPage": false,
    "hasNextPage": false,
    "prevPage": null,
    "nextPage": null
}

Without paginate it's working, but after using paginate it's display all the data. Any help ?

aravindnc commented 4 years ago

Can you try adding unwind:$track to the pipeline.

ibrahimsalah007 commented 4 years ago

same problem here it acts like query = {}

satish-baghel commented 3 years ago

Same here please solve this problem

satish-baghel commented 3 years ago

remove await in trade aggregate

>  const trayekAggregate = Trayek.aggregate([
>     {
>       $match: {
>         $or: [
>           {
>             'track.destination.destinationName': {
>               $regex: `${req.query.placeName}`,
>               $options: 'i',
>             },
>           },
>           {
>             'track.destination.destinationAddress': {
>               $regex: `${req.query.placeName}`,
>               $options: 'i',
>             },
>           },
>         ],
>       },
>     },
>     {
>       $project: {
>         destination: '$track.destination.destinationName',
>         destinationAddress: '$track.destination.destinationAddress',
>       },
>     },
>   ]);
aravindnc commented 2 years ago

Please reopen if the issue exists.