0
我想創建一個聚合查詢,以顯示每個城市患者最多的醫生。這也適用於每個城市的平均年齡。根據每個城市有多少患者來查找醫生的最高發生率
例如:
{
City: "Vancouver"
Physician: "Physician Test1"
Average_Age: 56
},
{
City: "Burnaby"
Physician: "Physician Test2"
Average_Age: 40
}
我的一些數據舉例
{
"gender" : "Female",
"full_name" : "Test Patient",
"age" : 20,
"city" : "Burnaby",
"severity_code" : 2,
"PHN" : "11-1111111",
"primary_physician" : "Physician Test1",
"location" : "4A",
"attendance_method" : "Self-Driven"
},
{
"gender" : "Male",
"full_name" : "Test2 Patient2",
"age" : 68,
"city" : "Vancouver",
"severity_code" : 1,
"PHN" : "00-0000000",
"primary_physician" : "Physician Test2",
"location" : "6D",
"attendance_method" : "Walk-In"
}
如何創建針對上述聚集查詢?
我對您提供的查詢中的$ sort:{count:-1}}感到困惑。這與這種聚合有什麼關係? –
具有'{「$ sort」:{「count」:-1}}的管道階段通過count(發生)命令先前分組的文檔。這對於進一步的分組是必要的,因爲您希望獲得每個城市中患者最多的醫師,即每個組中的患者,獲取計數最多的頂級文檔。前面的管道確實如此。 – chridam