Paginate数组在文件mongodb聚合中

问题描述 投票:0回答:1

我有用户集合,每个文档都有子文档(跟随/关注者),我想创建API以获取用户的关注者

此查询工作正常但我正在尝试创建一个分页结果,例如每页获得10个结果

{
        $match: { username: new RegExp(username, "i") }
      },
      {
        $unwind: "$followers"
      },
      {
        $lookup: {
          from: "users",
          localField: "followers",
          foreignField: "_id",
          as: "info"
        }
      },
      {
        $unwind: "$info"
      },
      {
        $project: {
          info: {
            _id: 1,
            username: 1,
            avatar: { $ifNull: ["$info.avatar", ""] },
            fullname: { $ifNull: ["$info.fullname", ""] },
            follow_status: {
              $cond: [{ $in: ["$info._id", "$followed"] }, 1, 0]
            }
          }
        }
      },
      {
        $replaceRoot: { newRoot: "$info" }
      } 

感谢您的支持

我使用https://github.com/NetanelBasal/paginate-array库在聚合阶段后对数组进行分页。

mongodb mongoose aggregation-framework
1个回答
0
投票

https://www.npmjs.com/package/mongoose-paginate这个插件库是我用来分页所有需要它的API调用的,它可以是超级基本的或超级复杂的,很容易插入到前端以及

© www.soinside.com 2019 - 2024. All rights reserved.