节点分页和过滤未按预期工作

Node Pagination and Filtering not working as Intended

我正在尝试在后端实现 PaginationFiltering

该控制器的输入是页码和过滤条件。

控制器:-

const getPosts = asyncHandler(async (req, res) => {
    const {
      page,
      statusFilter,
      typeFilter,
      sourceFilter,
    } = JSON.parse(req.query.filterData);
  
    
  
    var query = [
      {
        $addFields: {

          paramType: typeFilter,
          paramSource: sourceFilter,
          paramStatus: statusFilter,
        },
      },
      {
        $match: {
          $expr: {
            $and: [
              { user: req.user.id },
              {
                $or: [
                  {
                    $eq: ["$paramType", "All"],
                  },
                  {
                    $eq: ["$paramType", "$type"],
                  },
                ],
              },
              {
                $or: [
                  {
                    $eq: ["$paramSource", "All"],
                  },
                  {
                    $eq: ["$paramSource", "$source"],
                  },
                ],
              },
              {
                $or: [
                  {
                    $eq: ["$paramStatus", "All"],
                  },
                  {
                    $eq: ["$paramStatus", "$status"],
                  },
                ],
              },
            ],
          },
        },
      },
      {
        $project: {
          paramSource: false,
          paramType: false,
          paramStatus: false,
        },
      },
    ];
  
    //pagination
    const PAGE_SIZE = 5;
    const PAGE = parseInt(page) || 0;
  
    // aggregate query
    const aggregateQuery = await Post.aggregate([query]);

  
    const total = aggregateQuery.length;
  
  
    const Allposts = await Post.aggregate([query])
      .limit(PAGE_SIZE)
      .skip(PAGE_SIZE * PAGE)
      .sort({ createdAt: -1 });
  
    const totalPages = Math.ceil(total / PAGE_SIZE);
  
  
    res.status(200).json({ totalPages, Allposts });
  });

问题:-

分页和过滤部分按预期工作,但仅适用于第一页,当我转到第二页时page Allposts 对象 为空。

为什么 Allposts 对象在第一页后为空?

编辑:-

示例数据:-

{
    "_id" : 1,
    "type" : "Type A",
    "source" : "Source A",
    "status" : "Status A",
    "createdAt" : ISODate("2022-04-13T17:12:28.096Z"),
    "updatedAt" : ISODate("2022-04-13T17:12:28.096Z"),
    "__v" : 0
},
{
    "_id" : 2,
    "type" : "Type B",
    "source" : "Source C",
    "status" : "Status B",
    "createdAt" : ISODate("2022-04-13T17:12:28.096Z"),
    "updatedAt" : ISODate("2022-04-13T17:12:28.096Z"),
    "__v" : 0
},

{
    "_id" : 3,
    "type" : "Type A",
    "source" : "Source A",
    "status" : "Status A",
    "createdAt" : ISODate("2022-04-13T17:12:28.096Z"),
    "updatedAt" : ISODate("2022-04-13T17:12:28.096Z"),
    "__v" : 0
},

{
    "_id" : 4,
    "type" : "Type A",
    "source" : "Source C",
    "status" : "Status B",
    "createdAt" : ISODate("2022-04-13T17:12:28.096Z"),
    "updatedAt" : ISODate("2022-04-13T17:12:28.096Z"),
    "__v" : 0
}

更新聚合查询:-

var query = [
    
    {
      $addFields: {
        paramType: typeFilter,
        paramSource: sourceFilter,
        paramStatus: statusFilter,
      },
    },
    {
      $match: {
        $expr: {
          $and: [
            { user: req.user.id },
            {
              $or: [
                {
                  $eq: ["$paramType", "All"],
                },
                {
                  $eq: ["$paramType", "$type"],
                },
              ],
            },
            {
              $or: [
                {
                  $eq: ["$paramSource", "All"],
                },
                {
                  $eq: ["$paramSource", "$source"],
                },
              ],
            },
            {
              $or: [
                {
                  $eq: ["$paramStatus", "All"],
                },
                {
                  $eq: ["$paramStatus", "$status"],
                },
              ],
            },
            
          ],
        },
      },
    },{ $sort : { createdAt : -1 } },
    {
      $project: {
        paramSource: false,
        paramType: false,
        paramStatus: false,
        createdAt : 1,
      },
    }
  ];

如果你想分页+总计数一次查询,你可以这样做:

db.collection.aggregate([
  {
    $addFields: {
      paramType: "typeFilter",
      paramSource: "sourceFilter",
      paramStatus: "statusFilter"
    }
  },
  {
    "$match": {
      // complete here
    }
  },
  {
    $setWindowFields: {
      output: {totalCount: {$count: {}}}}
  },
  {$sort: {createdAt: -1}},
  {$skip: PAGE_SIZE * PAGE},
  {$limit: PAGE_SIZE},
  {
    $facet: {
      results: [
        {
          $project: {
            // here put whatever you want to send to FE
            type: 1,
            source: 1,
          }
        }
      ],
      totalCount: [
        {$limit: 1},
        {$project: {totalCount: 1, _id: 0}}
      ]
    }
  }
])

正如您在 playground

上看到的

$setWindowFields 允许您将总计数添加到所有文档。 $sort$skip$limit 允许分页。 $facet 允许您从相同的文档中获得不同的输出。