簡體   English   中英

如何獲得數組中值的平均值?

[英]How can I get the average of a value in an array?

在這段代碼中,我有一個Doctor文檔,並且文檔中有一系列的醫生患者。在這種情況下,我想找出我的所有患者的平均年齡,我該怎么做?

  {
            "_id" : ObjectId("57113238bde91693e9ff69e7"),
            "docname" : "Arthur Hovsepyan",
            "job_desc" : "Hepatologist",
            "sex" : "male",
            "jobtype" : "fulltime",
            "office" : "room 448",
            "email" : "arturchik@hotmail.com",
            "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"
                    }
            ]
    }

對於此問題,您將必須先解散患者的內部陣列,然后對患者 .age屬性應用$ avg運算符。 您的查詢將是:-

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

        }
])

如果您擁有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" : "arturchik@hotmail.com", 
    "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
}

您可以使用.aggregate()方法執行此操作,該方法提供對聚合管道的訪問。 話雖如此,如果您使用的是MongoDB 3.2或更高版本,則最佳方法是在$project階段使用$avg累加器運算符。

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

從MongoDB 3.0向后,您需要一種不同的方法。 您可以先$project然后使用$map運算符返回“患者”的“年齡”數組。 從那里開始,您將需要使用$unwind運算符對該數組進行反規范化,然后通過_id對文檔進行$group ,並使用$avg運算符返回“ age”的平均值。

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 
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM