Mongodb中几种聚合查询组合的案例

来源:互联网 发布:sqlserver未安装 编辑:程序博客网 时间:2024/05/29 11:11
db.onePager2ReadHistory.aggregate([    {$match: {'userId': '55054a56ad274f0b473fad39', 'isDeleted': 0}},    {$unwind: '$articleTags'},    {        $group: {            '_id': {'tagName': '$userId'},            'firstTimeReadingPoints': {                $sum: {                    $cond: [                        '$isLongTimeReading',                        4,                        {                            $cond: [ {$gt: ['$readCount', 0]}, 3, 0 ]                        }                    ]                }            },            'praisePoints': {                $sum: {                    $cond: [ {$eq: ['$like', 1]}, 3, 0 ]                }            },            'repeatReadingPoints': {                $sum: {                    $cond: [                        {$gt: ['$readCount', 1]},                        {                            $multiply: [                                6,                                {                                    $subtract: ['$readCount', 1]                                }                            ]                        },                        0                    ]                }            }        }    },    {        $project: {            '_id': 0,            'tagName': '$_id.tagName',            'firstTimeReadingPoints': 1,            'praisePoints': 1,            'repeatReadingPoints': 1,            'weight': {                $divide: [                    {$add: ['$firstTimeReadingPoints', '$praisePoints', '$repeatReadingPoints']},                    10                ]            }        }    }])

原创粉丝点击