获得分组值Mongoose的$ sum

时间:2016-11-21 18:18:09

标签: node.js mongodb mongoose

我有这样的查询:

{"$match": {$or: [{"to": system.mongoose.Types.ObjectId(userId)}, {"from": system.mongoose.Types.ObjectId(userId)}]}},
        {"$sort": {"createDate": -1}},
        {
            "$group": {
                "_id": "$conversationId",
                "from": {"$first": "$from"},
                "to": {"$first": "$to"},
                "content": {"$first": "$content"},
                "createDate": {"$first": "$createDate"},
                "unreaded": {"$sum": {"$cond":
                                {if :
                                            {
                                                $and: [
                                                    {
                                                        "$eq": [
                                                            "$unreaded", 1
                                                        ]
                                                    },
                                                    {
                                                        "$eq": ["$to", system.mongoose.Types.ObjectId(userId)]
                                                    }
                                                ]
                                            }, then: 1, else: 0}}}

unreaded会为每个会话返回未审核的邮件,如何获取所有unreaded条消息的总和?

0 个答案:

没有答案