景区 景区评论分组查询

景区、评论 操作

  • 查询语句
db.spot.aggregate(
    {
        $lookup:{
            from:'spot_comment_1',
            localField:'ota_spot_id',
            foreignField:'ota_spot_id',
            as: 'spot_comment'
        }
    },
    {
    $unwind: {
        path: "$spot_comment",
        preserveNullAndEmptyArrays: true
    }
    },
    {
        $project:{
                ota_spot_id : '$ota_spot_id',
                spot_name : '$spot_name',
                create_at: '$spot_comment.create_at',
                ota_spot_1515791 : {$cond : [{$eq : ['$spot_comment.ota_spot_id',1515791]},1,0]},
                ota_spot_30067 : {$cond : [{$eq : ['$spot_comment.ota_spot_id',30067]},1,0]},
                ota_spot_339 : {$cond : [{$eq : ['$spot_comment.ota_spot_id',339]},1,0]},
                ota_spot_5427075 : {$cond : [{$eq : ['$spot_comment.ota_spot_id',5427075]},1,0]},
                ota_spot_127339 : {$cond : [{$eq : ['$spot_comment.ota_spot_id',127339]},1,0]},
                ota_spot_62931 : {$cond : [{$eq : ['$spot_comment.ota_spot_id',62931]},1,0]},
                ota_spot_100025 : {$cond : [{$eq : ['$spot_comment.ota_spot_id',100025]},1,0]},
                ota_spot_103113 : {$cond : [{$eq : ['$spot_comment.ota_spot_id',103113]},1,0]}
        }
    },
    {
        $match:{
                $and:[
                    {$or:[
                        {'ota_spot_id':{$eq:103113}},
                        {'ota_spot_id':{$eq:100025}},
                        {'ota_spot_id':{$eq:5427075}},
                        {'ota_spot_id':{$eq:339}}
                    ]},
                    {'create_at':{$gte:'2017-12-04'}}
                ]
        }
    },
    {
        $group:{
            _id:{ota_spot_id:'$ota_spot_id','spot_name':'$spot_name'},
            ota_spot_1515791_count : {$sum : '$ota_spot_1515791'},
            ota_spot_30067_count : {$sum : '$ota_spot_30067'},
            ota_spot_339_count : {$sum : '$ota_spot_339'},
            ota_spot_5427075_count : {$sum : '$ota_spot_5427075'},
            ota_spot_127339_count : {$sum : '$ota_spot_127339'},
            ota_spot_62931_count : {$sum : '$ota_spot_62931'},
            ota_spot_100025_count : {$sum : '$ota_spot_100025'},
            ota_spot_103113_count : {$sum : '$ota_spot_103113'}
        }
    }
)
  • 返回结果
// 1
{
    "_id": {
        "ota_spot_id": NumberInt("339"),
        "spot_name": "石燕湖生态旅游公园"
    },
    "ota_spot_1515791_count": 0,
    "ota_spot_30067_count": 0,
    "ota_spot_339_count": 75,
    "ota_spot_5427075_count": 0,
    "ota_spot_127339_count": 0,
    "ota_spot_62931_count": 0,
    "ota_spot_100025_count": 0,
    "ota_spot_103113_count": 0
}

// 2
{
    "_id": {
        "ota_spot_id": NumberInt("5427075"),
        "spot_name": "石牛寨国家地质公园"
    },
    "ota_spot_1515791_count": 0,
    "ota_spot_30067_count": 0,
    "ota_spot_339_count": 0,
    "ota_spot_5427075_count": 49,
    "ota_spot_127339_count": 0,
    "ota_spot_62931_count": 0,
    "ota_spot_100025_count": 0,
    "ota_spot_103113_count": 0
}

// 3
{
    "_id": {
        "ota_spot_id": NumberInt("100025"),
        "spot_name": "长沙石燕湖"
    },
    "ota_spot_1515791_count": 0,
    "ota_spot_30067_count": 0,
    "ota_spot_339_count": 0,
    "ota_spot_5427075_count": 0,
    "ota_spot_127339_count": 0,
    "ota_spot_62931_count": 0,
    "ota_spot_100025_count": 219,
    "ota_spot_103113_count": 0
}

// 4
{
    "_id": {
        "ota_spot_id": NumberInt("103113"),
        "spot_name": "玻璃桥景区石牛寨"
    },
    "ota_spot_1515791_count": 0,
    "ota_spot_30067_count": 0,
    "ota_spot_339_count": 0,
    "ota_spot_5427075_count": 0,
    "ota_spot_127339_count": 0,
    "ota_spot_62931_count": 0,
    "ota_spot_100025_count": 0,
    "ota_spot_103113_count": 8
}

你可能感兴趣的:(景区 景区评论分组查询)