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 aggregatePaginate show all docs #31

Closed satish-baghel closed 3 years ago

satish-baghel commented 3 years ago

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

 var options = {
            page: page,
            limit: limit,
          }
          const productAggregate = await productDB.aggregate([
            {
              $match: {
                flag: { $in: [1] },
                name: { $regex: search, $options: 'i' },
              },
            },
            {
              $lookup: {
                from: 'categories',
                localField: 'category',
                foreignField: '_id',
                as: 'category',
              },
            },

            {
              $lookup: {
                from: 'users',
                localField: 'sellerId',
                foreignField: '_id',
                as: 'sellerId',
              },
            },

            {
              $lookup: {
                from: 'brands',
                localField: 'brand',
                foreignField: '_id',
                as: 'brand',
              },
            },

            {
              $sort: { createdAt: -1 },
            },
          ])
          productDB.aggregatePaginate(productAggregate, options).then((ress) => {
              res.status(200).json({ message: 'Get All Product', result: ress })
            })

Without paginate the result is something like this:

 [
        {
                        "_id": "5fdc4bb2727df931f84f9816",
                        "quantity": 10,
                        "tags": [
                            "check, new, done,change"
                        ],
                        "flag": 1,
                        "sellerId": "5fd9e432da66fb2480e1d192",
                        "name": "Name Change",
                        "productImage": [
                            {
                                "_id": "5fdc9227e5acf447682b6ea8",
                                "img": "uploads\\product\\wnrsV-app-6.jpg"
                            }
                        ],
                        "category": "5fdb166630012f276c7648ae",
                        "brand": "5fdb1c79a9a64142e895fa57",
                        "color": "Red",
                        "description": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book",
                        "price": 500,
                        "weight": "150 g",
                        "shipType": "Standard Shipping",
                        "createdAt": "2020-12-18T06:26:58.525Z",
                        "updatedAt": "2020-12-18T11:27:35.214Z",
                        "__v": 0
                    },
        ]

After use paginate, it displays all the data in the collection:

  {
        "message": "Get All Product",
        "result": {
            "docs": [
                {
                    "_id": "5fdc4bb2727df931f84f9816",
                    "quantity": 10,
                    "tags": [
                        "check, new, done,change"
                    ],
                    "flag": 1,
                    "sellerId": "5fd9e432da66fb2480e1d192",
                    "name": "Name Change",
                    "productImage": [
                        {
                            "_id": "5fdc9227e5acf447682b6ea8",
                            "img": "uploads\\product\\wnrsV-app-6.jpg"
                        }
                    ],
                    "category": "5fdb166630012f276c7648ae",
                    "brand": "5fdb1c79a9a64142e895fa57",
                    "color": "Red",
                    "description": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book",
                    "price": 500,
                    "weight": "150 g",
                    "shipType": "Standard Shipping",
                    "createdAt": "2020-12-18T06:26:58.525Z",
                    "updatedAt": "2020-12-18T11:27:35.214Z",
                    "__v": 0
                },
                {
                    "_id": "5fdc65be6a173f0048f982ef",
                    "quantity": 10,
                    "tags": [
                        "realme",
                        "phone"
                    ],
                    "flag": 1,
                    "sellerId": "5fd9e432da66fb2480e1d192",
                    "name": "Mobile",
                    "productImage": [
                        {
                            "_id": "5fdc65be6a173f0048f982f0",
                            "img": "uploads\\product\\b6VUS-app-6.jpg"
                        },
                        {
                            "_id": "5fdc65be6a173f0048f982f1",
                            "img": "uploads\\product\\ApKG0-app-5.png"
                        }
                    ],
                    "category": "5fdb166630012f276c7648ae",
                    "brand": "5fdb1c79a9a64142e895fa57",
                    "color": "Red",
                    "description": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book",
                    "price": 500,
                    "weight": "150 g",
                    "shipType": "Standard Shipping",
                    "createdAt": "2020-12-18T08:18:06.746Z",
                    "updatedAt": "2020-12-18T08:18:06.746Z",
                    "__v": 0
                }
            ],
            "totalDocs": 2,
            "limit": 10,
            "page": 1,
            "totalPages": 1,
            "pagingCounter": 1,
            "hasPrevPage": false,
            "hasNextPage": false,
            "prevPage": null,
            "nextPage": null
        }
    }
taitai3838 commented 3 years ago

i got this issue too i try several time and find my solution

you shouldn't await aggregate function make it pipeline and your result will come out

module.exports.aggregate = async ( aggregate ) => { const aggregateModel = mongoModel.aggregate(aggregate); // dont await here const paginate = await mongoModel.aggregatePaginate(aggregateModel,); return aggregateModel; };

here my code hope it'll help you

jaypeetancero commented 3 years ago

i got this issue too i try several time and find my solution

you shouldn't await aggregate function make it pipeline and your result will come out

module.exports.aggregate = async ( aggregate ) => { const aggregateModel = mongoModel.aggregate(aggregate); // dont await here const paginate = await mongoModel.aggregatePaginate(aggregateModel,); return aggregateModel; };

here my code hope it'll help you

Awesome!!, Works for me. Thank you.