Mongo aggregation with paginated data and totals

前端 未结 3 1257
日久生厌
日久生厌 2020-12-28 11:00

I\'ve crawled all over stack overflow, and have not found any info on how to return proper pagination data included in the resultset.

I\'m trying to aggregate some

3条回答
  •  执念已碎
    2020-12-28 11:26

    If you have a lot of events, {$ push: "$$ ROOT"}, will make Mongo return an error, I have solved it with $facet (Only works with version 3.4+)

    aggregate([
        { $match: options },
        {
          $facet: {
            edges: [
              { $sort: sort },
              { $skip: skip },
              { $limit: limit },
            ],
            pageInfo: [
              { $group: { _id: null, count: { $sum: 1 } } },
            ],
          },
        },
      ])
    

提交回复
热议问题