Mongodb 如何检查当前用户';在将投票分组并在同一聚合函数中求和之前进行s投票

Mongodb 如何检查当前用户';在将投票分组并在同一聚合函数中求和之前进行s投票,mongodb,aggregation-framework,mongodb-aggregation,Mongodb,Aggregation Framework,Mongodb Aggregation,我成功地获得了同一职位的所有选票的总和。 现在,我想看看当前用户(userId)是否也对给定的帖子投了票,然后返回他们的投票方式(+1或-1)以及对特定帖子的所有投票总数 是否可以这样做,或者我必须在聚合管道之外——在第二次查询中这样做?再次查询集合似乎很费力。是的,这是可能的。在管道中,您可以使用操作符作为向accum提供数据的逻辑运算符。例如: //pass data from Post query to PostVote sum function: PostVoteSchema.stat

我成功地获得了同一职位的所有选票的总和。 现在,我想看看当前用户(userId)是否也对给定的帖子投了票,然后返回他们的投票方式(+1或-1)以及对特定帖子的所有投票总数


是否可以这样做,或者我必须在聚合管道之外——在第二次查询中这样做?再次查询集合似乎很费力。

是的,这是可能的。在管道中,您可以使用操作符作为向accum提供数据的逻辑运算符。例如:

//pass data from Post query to PostVote sum function:

PostVoteSchema.statics.sum = function (data, userId) {

 var postIds = data.map(function (a) {
    return a._id;
  });

return PostVote
.aggregate(
    [
   { $match: { 'post': {$in: postIds}}},
   { $group: { _id:'$post' ,vote:{$sum:'$vote'}}}
 ])
.execAsync()
.then(function(votes){

    return votes;

   //desired output to client, _id is for specific post
   {_id: 5802ea4bc00cb0beca1972cc, vote: 3, currentUserVote: -1}

 });
};

是的,这是可能的。在管道中,您可以使用运算符作为向累加器运算符馈电的逻辑。例如:

//pass data from Post query to PostVote sum function:

PostVoteSchema.statics.sum = function (data, userId) {

 var postIds = data.map(function (a) {
    return a._id;
  });

return PostVote
.aggregate(
    [
   { $match: { 'post': {$in: postIds}}},
   { $group: { _id:'$post' ,vote:{$sum:'$vote'}}}
 ])
.execAsync()
.then(function(votes){

    return votes;

   //desired output to client, _id is for specific post
   {_id: 5802ea4bc00cb0beca1972cc, vote: 3, currentUserVote: -1}

 });
};

但是,这个“仅”会返回用户的总数吗?是的,因为
uservoces
字段仅对
userId
所做的投票进行有条件地求和。是否要返回所有与postId匹配的投票,然后以某种方式标记同一管道中存在userId的post?可能是第二组。您能为其创建一个新问题吗这可能会显示一些示例文档和聚合查询的预期输出?好吧,从技术上讲,这是我的原始问题。是否最好为此添加更多细节或添加新的细节?这是否“仅”返回用户的总和?是的,因为
uservoces
字段仅对
userId
。是否要返回所有与postId匹配的投票,然后以某种方式标记同一管道中存在userId的post?可能是第二组。您能否为此创建一个新问题,可能显示一些示例文档和聚合查询的预期输出?好的,这是我的原始问题打开。是否最好在此添加更多详细信息或添加一个新的?您是否还可以显示某些给定示例文档的预期输出?当然,我将添加预期的JSONC您还可以显示某些给定示例文档的预期输出?当然,我将添加预期的JSON
//pass data from Post query to PostVote sum function:

PostVoteSchema.statics.sum = function (data, userId) {

 var postIds = data.map(function (a) {
    return a._id;
  });

return PostVote
.aggregate(
    [
   { $match: { 'post': {$in: postIds}}},
   { $group: { _id:'$post' ,vote:{$sum:'$vote'}}}
 ])
.execAsync()
.then(function(votes){

    return votes;

   //desired output to client, _id is for specific post
   {_id: 5802ea4bc00cb0beca1972cc, vote: 3, currentUserVote: -1}

 });
};
return PostVote.aggregate([
   { "$match": { "post": { "$in": postIds } } },
   { 
        "$group": {
            "_id": "$post",
            "votes": { "$sum": "$vote" },                 
            "userVotes": {
                "$sum": {
                    "$cond": [
                        { "$eq": ["$user", userId] },
                        "$vote",
                        0
                    ]
                }
            }
        }
    }
 ]).execAsync().then(function(votes){
    return votes;
});