2016-04-16 79 views
0

在这段代码中,我有一个Doctor文档,文档里面有一个医生的病人列表。在这种情况下,我想查看我阵列中所有患者的平均年龄。我该怎么做?如何获得数组中值的平均值?

{ 
      "_id" : ObjectId("57113238bde91693e9ff69e7"), 
      "docname" : "Arthur Hovsepyan", 
      "job_desc" : "Hepatologist", 
      "sex" : "male", 
      "jobtype" : "fulltime", 
      "office" : "room 448", 
      "email" : "[email protected]", 
      "phone_number" : 862124343, 
      "address" : "68 Peterburg street,waterford", 
      "hours" : 12, 
      "patients" : [ 
        { 
          "name" : "Jenny Power", 
          "ward_no" : 1, 
          "sex" : "female", 
          "termdays" : 2, 
          "illness_type" : "minor", 
          "age" : 22, 
          "phone_number" : 877285221, 
          "address" : "63 Johnston street ,Waterford" 
        }, 
        { 
          "name" : "Marie Peters", 
          "ward_no" : 2, 
          "sex" : "female", 
          "termdays" : 0, 
          "illness_type" : "minor", 
          "age" : 21, 
          "phone_number" : 862145992, 
          "address" : "99 Grange,Waterford" 
        }, 
        { 
          "name" : "Philip John", 
          "ward_no" : 2, 
          "sex" : "male", 
          "termdays" : 10, 
          "illness_type" : "serious", 
          "age" : 31, 
          "phone_number" : 861125981, 
          "address" : "12 Monvoy Bridge,Waterford" 
        }, 
        { 
          "name" : "Marta Peters", 
          "ward_no" : 3, 
          "sex" : "female", 
          "termd7ays" : 0, 
          "illness_type" : "minor", 
          "age" : 31, 
          "phone_number" : 862125981, 
          "address" : "100 Grange Manor,Waterford" 
        } 
      ] 
    } 

回答

0

可以使用.aggregate()方法可用于访问聚合管道做到这一点。话虽如此,如果您使用的是MongoDB 3.2或更新的版本,那么最佳方法是在$project阶段使用$avg累加器运算符。

db.collection.aggregate([ 
    { "$project": { 
     "averageAge": { "$avg": "$patients.age" } 
    }} 
]) 

从MongoDB的3.0向后你需要不同的方法。您可以首先使用$map操作员$project您的文档并返回一组“患者”的“年龄”。从那里你需要使用$unwind运营商然后$group您的文档_id去规范该阵列,并使用$avg运算符返回“年龄”的平均值。

db.collection.aggregate([ 
    { "$project": { 
     "agePatients": { 
      "$map": { 
       "input": "$patients", 
       "as": "p", 
       "in": "$$p.age" 
      } 
     } 
    }}, 
    { "$unwind": "$agePatients" }, 
    { "$group": { 
     "_id": "$_id", 
     "averageAge": { "$avg": "$agePatients" } 
    }} 
]) 

将返回:

{ 
    "_id" : ObjectId("57113238bde91693e9ff69e7"), 
    "averageAge" : 26.25 
} 
0

对于这个问题,你必须先解开患者的内部数组,然后应用$平均操作上patients.age属性。您所查询的是: -

db. collection.aggregate([ 
    { 
    "$unwind": "$patients" 
    }, 
    { 
     $group : { 
       _id:{ 
         "docname" : "$docname" 
        }, 
       avg_age : {$avg : "$patients.age"} 
      } 

     } 
]) 
+0

当我尝试,我得到的零平均 – ebatinstitute

0

如果你有MongoDB的3.2+,你可以在其中放少了紧张的服务器资源$project阶段使用$avg。但是,如果您的MongoDB版本低于3.2,请考虑ashisahu发布的解决方案。

db.collection.aggregate([ 
    { 
    $project:{ 
     docname: 1, 
     job_desc: 1, 
     sex: 1, 
     jobtype: 1, 
     office: 1, 
     email: 1, 
     phone_number: 1, 
     address: 1, 
     hours: 1, 
     patients: 1, 
     avg_age_of_patients:{$avg:"$patients.age"} 
    } 
    } 
]) 

这将输出以下文件。 (见avg_age_of_patients场)

{ 
    "_id" : ObjectId("57113238bde91693e9ff69e7"), 
    "docname" : "Arthur Hovsepyan", 
    "job_desc" : "Hepatologist", 
    "sex" : "male", 
    "jobtype" : "fulltime", 
    "office" : "room 448", 
    "email" : "[email protected]", 
    "phone_number" : 8.62124343E8, 
    "address" : "68 Peterburg street,waterford", 
    "hours" : 12.0, 
    "patients" : [ 
     { 
      "name" : "Jenny Power", 
      "ward_no" : 1.0, 
      "sex" : "female", 
      "termdays" : 2.0, 
      "illness_type" : "minor", 
      "age" : 22.0, 
      "phone_number" : 8.77285221E8, 
      "address" : "63 Johnston street ,Waterford" 
     }, 
     { 
      "name" : "Marie Peters", 
      "ward_no" : 2.0, 
      "sex" : "female", 
      "termdays" : 0.0, 
      "illness_type" : "minor", 
      "age" : 21.0, 
      "phone_number" : 8.62145992E8, 
      "address" : "99 Grange,Waterford" 
     }, 
     { 
      "name" : "Philip John", 
      "ward_no" : 2.0, 
      "sex" : "male", 
      "termdays" : 10.0, 
      "illness_type" : "serious", 
      "age" : 31.0, 
      "phone_number" : 8.61125981E8, 
      "address" : "12 Monvoy Bridge,Waterford" 
     }, 
     { 
      "name" : "Marta Peters", 
      "ward_no" : 3.0, 
      "sex" : "female", 
      "termd7ays" : 0.0, 
      "illness_type" : "minor", 
      "age" : 31.0, 
      "phone_number" : 8.62125981E8, 
      "address" : "100 Grange Manor,Waterford" 
     } 
    ], 
    "avg_age_of_patients" : 26.25 
}