如何从Mongodb的生日起计算平均年龄?

时间:2019-07-28 15:36:57

标签: mongodb mongodb-query aggregation-framework

我有一个客户集合,我需要计算每种性别的客户平均年龄

客户数据:

{"Id_Cust":"4145","firstName":"Albade","lastName":"Maazou","gender":"female","birthday":"21/03/1981","creationDate":"2010-03-13T02:10:23.099+0000","locationIP":"41.138.53.138","browserUsed":"Internet Explorer","place":"1263"},
{"Id_Cust":"5296","firstName":"Rafael","lastName":"Oliveira","gender":"male","birthday":"08/06/1987","creationDate":"2010-02-05T06:08:26.512+0000","locationIP":"192.160.128.234","browserUsed":"Internet Explorer","place":"574"},
{"Id_Cust":"6192","firstName":"Abdul Rahman","lastName":"Budjana","gender":"male","birthday":"26/01/1990","creationDate":"2010-02-08T20:32:23.706+0000","locationIP":"27.112.77.226","browserUsed":"Firefox","place":"640"},
{"Id_Cust":"6660","firstName":"Jerzy","lastName":"Ciesla","gender":"female","birthday":"28/12/1982","creationDate":"2010-03-23T11:02:30.998+0000","locationIP":"31.192.218.139","browserUsed":"Firefox","place":"1285"},
{"Id_Cust":"8491","firstName":"Chen","lastName":"Xu","gender":"female","birthday":"27/02/1985","creationDate":"2010-03-31T15:59:11.072+0000","locationIP":"1.1.7.155","browserUsed":"Chrome","place":"437"},
{"Id_Cust":"8664","firstName":"Andrej","lastName":"Benedejcic","gender":"female","birthday":"31/08/1988","creationDate":"2010-03-24T03:12:59.456+0000","locationIP":"90.157.195.42","browserUsed":"Firefox","place":"549"},
{"Id_Cust":"10027","firstName":"Ning","lastName":"Chen","gender":"female","birthday":"08/12/1982","creationDate":"2010-03-27T12:58:05.517+0000","locationIP":"1.2.9.86","browserUsed":"Firefox","place":"332"},
{"Id_Cust":"10664","firstName":"Emperor of Brazil","lastName":"Barreto","gender":"female","birthday":"02/02/1982","creationDate":"2010-03-04T09:43:10.090+0000","locationIP":"192.111.230.73","browserUsed":"Internet Explorer","place":"576"},
{"Id_Cust":"2199023256013","firstName":"Eli","lastName":"Peretz","gender":"female","birthday":"18/01/1989","creationDate":"2010-04-28T05:16:53.514+0000","locationIP":"193.194.1.47","browserUsed":"Internet Explorer","place":"1227"},

结果必须是这样的:

{ "_id" : "female" , "count" : 35}
{ "_id" : "male" , "count" : 35}

注意(以35为例)

3 个答案:

答案 0 :(得分:0)

使用$dateFromString将字符串转换为ISODate,然后可以在该日期运行$year运算符。要获得平均值,您需要gender之前$group,并使用$avg运算符

db.col.aggregate([
    {
        $project: {
            gender: 1,
            age: {
                $subtract: [ 2019, { $year: { $dateFromString: { dateString: "$birthday", format: "%d/%m/%Y" } } }]
            }
        }
    },
    {
        $group: {
            _id: "$gender",
            avgAge: { $avg: "$age" }
        }
    }
])

答案 1 :(得分:0)

假设集合名称为apple,则可以使用以下聚合查询:

  db.getCollection('apple').aggregate([
     {
        $match:{"birthday":{ $exists : true} } 
     },
    { $project : {gender:"$gender", age: { 
            $divide: [{$subtract: [ new Date(),  new Date("$birthday") ] }, 
                    (365 * 24*60*60*1000)]
        }} }, 
    {$group:{_id:{age:"$age",gender:"$gender"}}},
    {$project:{_id:0,"gender":"$_id.gender","avgAge":"$_id.age"}}
])

输出: enter image description here

答案 2 :(得分:0)

纯粹根据生日到今天的年数来进行年龄计算,应该执行以下操作:

db.tempColl.aggregate(
   [
     {
       $group:
         {
           _id: "$gender",
           count: { $avg: { $subtract: [ {$year: new Date()}, {$toInt:{$substr: ["$birthday", 6, -1]}} ] } }
         }
     }
   ]
);

输出如下:

{ "_id" : "male", "count" : 30.5 }
{ "_id" : "female", "count" : 34.857142857142854 }

注意:如果需要绝对数字,可以使用 $ ceil 运算符。这也假设您的生日值将采用给定的格式dd / mm / yyyy。而 tempColl 是您的收藏集。