美文网首页
MongoDB分组查询

MongoDB分组查询

作者: 陈文瑜 | 来源:发表于2019-10-07 15:21 被阅读0次

分组查询示例

db.spot_comment.aggregate([
    {
        $match: {
            $or: [{
                create_at: {
                    $eq: '2018-11-24'
                }},
                {create_at: {
                    $eq: '2018-11-13'
                }
            }]
        }
    },
    {
        $group: {
            _id: {
                c_score: "$c_score",
                create_at:"$create_at"
            },
            'count': {
                '$sum': 1
            }
        }
    }
])
分组获取最新的一条记录
db.spot_comment.aggregate([
{
    $group: {
        _id: {
                ota_id:"$ota_id",
                ota_spot_id:"$ota_spot_id"
                },
        "time": {
            $last: "$create_at"
        },
        "c_score": {
            $last: "$c_score"
        }
    }
}, {
    $sort: {
        "create_at":  - 1
    }
}])

相关文章

网友评论

      本文标题:MongoDB分组查询

      本文链接:https://www.haomeiwen.com/subject/iednectx.html