首页 > 解决方案 > 聚合中的分页,也在猫鼬中使用了 $project

问题描述

我想用投影显示对象的用户列表,还想显示我想要这样输出的总页面

{

"Success": true,
"message": " Fetched post comment successfully.",
"data": {
       "docs": [
        {
            "_id": "60101fcc4077e698facd63aa",
            "commentDetail": {
                "hasReply": false,
                "likeCount": 0,
                "angryCount": 0,
                "favCount": 0,
                "totalReaction": 0,
                "description": "four comment",
                "media": null,
                "date": "2021-01-26T13:57:32.220Z",
                "action": [],
                "commentReplies": []
            },
            "userID": "5f5238b5458b7c63a477bf87",
            "postID": "5fb7a19bcae255415e99781b",
            "commentID": "60101fcb4077e698facd63a9",

        },
{
            "_id": "60101fcc4077e698facd63aa",
            "commentDetail": {
                "hasReply": false,
                "likeCount": 0,
                "angryCount": 0,
                "favCount": 0,
                "totalReaction": 0,
                "description": "four comment",
                "media": null,
                "date": "2021-01-26T13:57:32.220Z",
                "action": [],
                "commentReplies": []
            },
            "userID": "5f5238b5458b7c63a477bf87",
            "postID": "5fb7a19bcae255415e99781b",
            "commentID": "60101fcb4077e698facd63a9",

        }
    ],
    "count": 1
}
}

我写这个查询

    getPostAllComment = await this.comment.aggregate([
            { $match: { postID: ObjectId(getPostCommentDTO.postID) } },
            { $sort: { createdAt: 1 } }, //sort on created At
            { $skip: (parseInt(getPostCommentDTO.pageNum) - 1) * parseInt(getPostCommentDTO.pageSize) },
            { $limit: parseInt(getPostCommentDTO.pageSize) },
            {
                $group: {
                    _id: "$postID",
                    docs: { $push: "$$ROOT" },

                    count: { $sum: 1 },
                },
            },
            { $project: { _id: 0 } 
}

它给了我上面期望的输出,但是我的 $project 在这里不起作用。所以我如何才能完全像上面一样显示输出,根级别的总页数字段不在带有投影的每个文档中。我已经尝试使用 $facet但它没有像我上面解释的那样给我输出

标签: node.jsmongoose

解决方案


我通过这个查询得到了我想要的结果

getPostAllComment = await this.comment.aggregate([
            { $match: { postID: ObjectId(getPostCommentDTO.postID) } },
            {
                $group: {
                    _id: null,
                    collection: { $push: "$$ROOT" },
                    count: { $sum: 1 },
                },
            },
            { $unwind: "$collection" },
            { $sort: { createdAt: 1 } }, //sort on created At
            { $skip: (parseInt(getPostCommentDTO.pageNum) - 1) * parseInt(getPostCommentDTO.pageSize) },
            { $limit: parseInt(getPostCommentDTO.pageSize) },
            {
                $project: {
                    hasReply: "$collection.commentDetail.hasReply",
                    likeCount: "$collection.commentDetail.likeCount",
                    angryCount: "$collection.commentDetail.angryCount",
                    favCount: "$collection.commentDetail.favCount",
                    totalReaction: "$collection.commentDetail.totalReaction",
                    date: "$collection.commentDetail.date",
                    media: "$collection.commentDetail.media",
                    description: "$collection.commentDetail.description",
                    action: "$collection.commentDetail.action",
                    recentCommentReplies: "$collection.commentDetail.commentReplies",
                    userProfilePicture: "$collection.userProfilePicture",
                    userProfilePictureThumbnail: "$collection.userProfilePictureThumbnail",
                    userName: "$collection.userName",
                    userID: "$collection.userID",
                    postID: "$collection.postID",
                    commentID: "$collection.commentID",
                    createdAt: "$collection.createdAt",
                    updatedAt: "$collection.updatedAt",
                    count: "$count",
                },
            },

            {
                $group: {
                    _id: "$postID",
                    docs: { $push: "$$ROOT" },
                    total: { $first: "$count" },
                },
            },
            { $project: { "docs.count": 0, _id: 0 } },
        ])
return getPostAllComment[0]

推荐阅读