Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
274 views
in Technique[技术] by (71.8m points)

node.js - Pagination in aggregation and also used $project in mongoose

i want to show user list of object with projection and also want to show the total page i want output like this

{

"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
}
}

i write this query

    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 } 
}

it give me output like above which i am expecting but my $project is not working here.so how can i show output exactly like above with total page count field at root level not in each document with projection.i already tried to used $facet but it not give me output like i above explain

question from:https://stackoverflow.com/questions/65898519/pagination-in-aggregation-and-also-used-project-in-mongoose

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I get my desire result by this query

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]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...