node.js - 聚合中的分页,也在猫鼬中使用了 $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但它没有像我上面解释的那样给我输出
解决方案
我通过这个查询得到了我想要的结果
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]
推荐阅读
- sql - 一个 liquibase 变更集中的 2 个先决条件
- python - 渲染()缺少 1 个必需的位置参数:'template_name'
- android - 如何在我的 Maven 本地存储库中使用 gradle 发布现有的 aar?
- angular - NGXS Devtools 不代表当前状态
- r - 如何将数据框或矩阵转换为表格
- codefluent - 如何从/向 Azure Blob 存储加载/保存 Blob?
- mysql - 将 Coldfusion 应用程序迁移到 Lucee
- ios - 以编程方式在中间设置框架 CGReact x 位置,如 centerXAnchor NSLayoutConstraint
- java - Spring框架邮件确认链接错误
- ios - Objective-C - UIAlertView 不委托