MongoDB-使用聚合管道进行评论投票/降票 [英] MongoDB - Comment Upvoting/Downvoting with Aggregation Pipeline
问题描述
我正在尝试对评论实施upvote/downvote机制(类似于reddit上的upvoting/downvoting机制).我有一个名为commentReputation
的单独集合,其中的文档如下所示:
I'm trying to implement an upvote/downvote mechanism for comments (similar to the upvoting/downvoting mechanism found on reddit). I have a separate collection called commentReputation
and the documents inside can look like this:
{
"_id" : ObjectId("5e5acb6d6034a879655c8819"),
"commentId" : ObjectId("5e5983102328a83d1a4b541f"),
"creationDate" : ISODate("2020-02-29T20:37:01.509Z"),
"upvotes" : [
ObjectId("5e5983102328a83d1a4b53e7"),
ObjectId("5e5983102328a83d1a4b53e4")
],
"downvotes" : [
ObjectId("5e5983102328a83d1a4b53e5")
]
}
简而言之:每个评论最终都会拥有它自己的CommentReputation
文档(CommentReputation
文档应在有人投票赞成/反对投票时立即创建)
In short: every comment will eventually have it's own CommentReputation
document (the CommentReputation
document should be created as soon as someone upvotes/downvotes a comment)
有2种情况:
-
该集合为空,这意味着我需要使用给定的commentId
x
创建我的第一个CommentReputation
文档.在项目的其他部分,我将$setOnInsert
与{ upsert: true }
结合使用,但(从文档中看)似乎聚合管道目前不支持$setOnInsert
.有没有其他方法可以解决这个问题?
The collection is empty meaning that I need to create my very first
CommentReputation
document with a given commentIdx
. In some other part of the project I was using$setOnInsert
with{ upsert: true }
but it seems (looking at the documentation) that the aggregation pipeline does not support$setOnInsert
as for now. Is there another way to deal with this problem?
文档在那里,应该进行实际的表决.
The document is there and the actuall upvoting should occur.
a)upvotes
和downvotes
数组均不包含试图进行投票的userId
,因此无需任何进一步操作即可将其添加到upvotes
数组中
a) Both upvotes
and downvotes
arrays do not contain the userId
that is trying to upvote thus it gets added to the upvotes
array without any further actions
b)upvotes
数组包含试图对注释进行投票的userId
,因此应从upvotes
数组中删除userId
. (用户已经对此评论进行了投票,并再次点击了取消投票"按钮,取消了该投票)
b) The upvotes
array contains the userId
that is trying to upvote the comment as a result the userId
should be REMOVED from the upvotes
array. (the user already had this comment upvoted and clicked a second time the upvote button which cancels out the upvote)
c)downvotes
数组包含userId
.在这种情况下,应从downvotes
中删除userId
并将其添加到upvotes
c) The downvotes
array contains the userId
. In this case the userId
should be removed from downvotes
and added to upvotes
我正在尝试使用updateOne
方法和聚集管道来完成上述逻辑,但是我不确定这是否可能.
I'm trying to accomplish the above logic with the updateOne
method and a aggreagtion pipeline however I'm not sure if this is even possible.
我目前拥有的是返回"Unrecognized pipeline stage name: '$cond'"
const updateUpvotes = {
$cond: {
if: { $elemMatch: { upvotes: ObjectID(userId) } },
then: { $pull: { upvotes: ObjectID(userId) } },
else: { $addToSet: { upvotes: ObjectID(userId) } }
}
};
db.collection(collectionName).updateOne({
commentId: ObjectID('5e5983102328a83d1a4b541f')
}, [updateUpvotes])
我是否对整个功能有过多的考虑?我想1.
问题可以通过简单地创建CommentReputation
文档(同时创建Comment
文档的同时包含空白upvotes
和downvotes
)来解决.
Am I overthinking the whole feature? I guess the 1.
problem can be solved by simply creating a CommentReputation
document (with empty upvotes
and downvotes
at the same time the Comment
document is being created.
是否有更好的方法?我很想让它在单个查询请求中运行.也许你们中的某人实现了类似的功能,并且可以给我一些提示.
Is there a better way of doing this? I would love to have it working inside a single query request. Maybe someone of You guys implemented a similar feature and can give me some hints on this one.
推荐答案
您可以通过以下管道更新来完成此操作,但它要求存在upvotes和downvotes数组.即使只是空的.
you can do it with the following pipeline update but it requires that the upvotes and downvotes arrays exist. even if it's just empty.
var comment_id = ObjectId("5e5983102328a83d1a4b541f");
var user_id = ObjectId("5e5983102328a83d1a4b53e5");
db.commentReputation.update(
{
commentId: comment_id
},
[
{
$set: {
upvotes: {
$cond: [
{ $in: [user_id, '$upvotes'] },
{ $setDifference: ['$upvotes', [user_id]] },
{ $setUnion: ['$upvotes', [user_id]] }
]
}
}
},
{
$set: {
downvotes: {
$cond: [
{ $in: [user_id, '$downvotes'] },
{ $setDifference: ['$downvotes', [user_id]] },
'$downvotes'
]
}
}
}
]
);
这篇关于MongoDB-使用聚合管道进行评论投票/降票的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!