2016-04-16 73 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 
}