您的位置:首页 > 数据库 > Mongodb

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

2017-07-14 08:50 579 查看
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
]
}
}
}
])
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: