我正在做一个项目,当用户喜欢posts集合中post文档的评论数组中嵌套的评论时,我使用以下代码来增加评论的点赞数量:
await Post.findOneAndUpdate({ "postComments.commentId": commentId }, { $inc: { "postComments.$.commentLikes": 1 } });
问题是,我需要我递增的原始数字将其返回给用户视图,以动态地递增显示的点赞数量,并且我无法获得整个对象,因为它将返回整个帖子,其中评论嵌套在评论数组中,而我不知道是哪个索引。如何从嵌套评论中获取原始的点赞数量?
提前感谢您:)
发布于 2021-10-10 06:15:03
在findOneAndUpdate中使用projection
db.getCollection('comment').findOneAndUpdate(
{
"postComments.commentId": "1"
},
{
$inc: {
"postComments.$.commentLikes": 1
}
},
{
projection: { postComments: { $elemMatch: { commentId: { $eq: "1" } } }}
})
https://stackoverflow.com/questions/69512178
复制相似问题