【问题标题】:Populate and Paginate an sub document on Mongoose在 Mongoose 上填充和分页子文档
【发布时间】:2020-08-03 19:01:16
【问题描述】:

我的帖子对象包含一个评论对象数组,我需要根据偏移量和限制(分页)填充评论。

它适用于 mongoose API,但我需要使用 mongoose-paginate-v2

    const post = await Post.findById(req.params.id)
        .populate("author")
        .populate("manufacturer")
        .populate({
            path: "comments",
            populate: "author",
            options: {
                sort: { created_at: -1 },
                skip: (pageNumber - 1) * limit,
                limit: limit,
            },
     })

它必须与 mongoose-paginate-v2 框架一起使用,因为我需要一些额外的属性,例如 total。

【问题讨论】:

    标签: node.js mongoose mongodb-query


    【解决方案1】:
    document.paginate(filter, options).then(async function(result)
    where in the option there will be 
    populate: {
            path : 'item1',
            populate : {
              path : 'item2'
            }
          },
    

    【讨论】:

    • 请注意,您不能使用 item1.item2,它必须为每个项目嵌套。我自己掉进了这个陷阱,不得不质疑。
    猜你喜欢
    • 2017-01-27
    • 1970-01-01
    • 2014-08-16
    • 2018-08-16
    • 2017-03-21
    • 2017-06-06
    • 2014-08-06
    • 2020-12-18
    • 2015-08-08
    相关资源
    最近更新 更多