如何从一个集合中具有不同状态的mongodb获取计数

时间:2018-06-25 08:54:40

标签: node.js mongodb mongoose aggregation-framework

我有约会集合,因为我有即将到来,已取消,已完成的状态代码。我想编写一个API,以使用mongoose或mongodb方法获取每个状态的计数。

输出应如下所示

[{
    group : "grp1",
    appointments_completed :4
    appointments_upcoming :5
    appointments_cancelled : 7
}]

提前谢谢。

3 个答案:

答案 0 :(得分:0)

希望对您有帮助

db.getCollection('codelist').aggregate([
{
    $group:{
    _id:{status:"$status"},
    count:{$sum:1}
    } 
}
])

结果将是

[{
    "_id" : {
        "status" : "cancelled"
    },
    "count" : 13.0
},
{
    "_id" : {
        "status" : "completed"
    },
    "count" : 20.0
}

]

我认为您可以使用nodejs处理它

答案 1 :(得分:0)

使用Aggregation Pipeline $group,我们可以得到此计数

db.collection_name.aggregate([ 
   { $group: { 
       _id:null, 
       appointments_completed: {$sum : "$appointments_completed" }, 
       appointments_upcoming:{$sum :"$appointments_upcoming"}, 
       appointments_cancelled:{$sum: "$appointments_cancelled"}
      }
    }
]);

答案 2 :(得分:0)

在MongoDb 3.6和更高版本中,您可以利用 $arrayToObject 运算符和 $replaceRoot 管道的使用来获得所需的结果。您将需要运行以下聚合管道:

db.appointments.aggregate([
    { "$group": {
        "_id": {  
            "group": <group_by_field>,
            "status": { "$concat": ["appointments_", { "$toLower": "$status" }] }
        },
        "count": { "$sum": 1 }
    } },
    { "$group": {
        "_id": "$_id.group",
        "counts": {
            "$push": {
                "k": "$_id.status",
                "v": "$count"
            }
        }
    } },
    { "$addFields": {
        "counts": {
            "$setUnion": [
                "$counts", [
                    {
                        "k": "group",
                        "v": "$_id"
                    }
                ]
            ]
        }
    } },
    {  "$replaceRoot": {
        "newRoot": { "$arrayToObject": "$counts" }
    } }    
])

对于较旧的版本,尽管输出格式不同,但更通用的方法是将分组两次并以键值对象数组的形式获取计数,如下所示:

db.appointments.aggregate([
    { "$group": {
        "_id": { 
            "group": <group_by_field>,
            "status": { "$toLower": "$status" }
        },
        "count": { "$sum": 1 }
    } },
    { "$group": {
        "_id": "$_id.group",
        "counts": {
            "$push": {
                "status": "$_id.status",
                "count": "$count"
            }
        }
    } }
])

会吐出:

{
    "_id": "grp1"
    "counts":[
        { "status": "completed", "count": 4 },
        { "status": "upcoming", "count": 5 }
        { "status": "cancelled", "count": 7 }
    ]
}

如果状态码是固定的,则$cond流水线步骤中的$group运算符可以有效地用于根据状态字段值评估计数。您的总体聚合管道可以按以下方式构造以产生所需格式的结果:

db.appointments.aggregate([    
    { "$group": { 
        "_id": <group_by_field>,             
        "appointments_completed": {
            "$sum": {
                "$cond": [ { "$eq": [ "$status", "completed" ] }, 1, 0 ]
            }
        },
        "appointments_upcoming": {
            "$sum": {
                "$cond": [ { "$eq": [ "$status", "upcoming" ] }, 1, 0 ]
            }
        },
        "appointments_cancelled": {
            "$sum": {
                "$cond": [ { "$eq": [ "$status", "cancelled" ] }, 1, 0 ]
            }
        }
    } }
])