2017-02-24 98 views
1

我正在使用以下mongo db查詢。它僅向我展示了我的人物角色數據的年齡組。按mongo db中的年齡組分組

db.amplifyindex.aggregate([ 
    { $unwind: "$demographic" }, 
    { $match : { 'demographic.is_latest':"active",   
    'demographic.date_of_birth' : { $exists : true} } }, 
    { $project : {"ageInMillis" : {$subtract : [new Date(), 
    "$demographic.date_of_birth"] } } }, 
    { $project : {"age" : {$divide : ["$ageInMillis", 31558464000] }}}, 
    { $project : {"age" : {$subtract : ["$age", {$mod : ["$age",1]}]}}}, 

    ]) 

此查詢結果對象是:

{ 
"_id" : ObjectId("58a42cbbdb5d880c1e000029"), 
"age" : 29.0 
} 

我希望的方式來獲得數據:

{ 
"_id" : ObjectId("58a42cbbdb5d880c1e000029"), 
"age" : 10-30 
"personas" : 10 
} 
在我的情況

完整的數據結構,給出如下:

{ 
"_id" : ObjectId("58a42cbbdb5d880c1e000029"), 
"persona_email" : "[email protected]", 
"company_id" : "1", 
"date_added" : ISODate("2017-02-15T10:23:15.000Z"), 
"demographic" : [ 
    { 
     "persona_fname" : "Hayden", 
     "middle_name" : "Jacobs", 
     "persona_lname" : "Schmeler", 
     "gender" : "male", 
     "date_of_birth" : ISODate("1987-06-16T19:00:00.000Z"), 
     "marital_status" : "single", 
     "height" : "2.1 feet", 
     "weight" : "5 kg", 
     "measurement" : { 
      "waist" : "34 inch" 
     }, 
     "disabilities" : "No", 
     "race" : "Asian", 
     "nationality" : "Jordan", 
     "life_cycle" : "empty nest", 
     "children" : { 
      "gender" : "female", 
      "date_of_birth" : ISODate("1987-06-16T19:00:00.000Z") 
     }, 
     "medicare_no" : "4916725587565", 
     "driving_licence_no" : "PACBALM420M", 
     "id_no" : "4485872783336", 
     "passport_no" : "OCRFHCNR", 
     "residential_address" : { 
      "unit_no" : 603, 
      "street_no" : "34863 Ondricka Viaduct Apt. 154", 
      "street_name" : "84799 Little Wall", 
      "suburb" : "West Virginia", 
      "postal_code" : "23873", 
      "state" : "Dominica", 
      "country" : "Cook Islands" 
     }, 
     "work_address" : { 
      "unit_no" : "6011865161287875", 
      "street_no" : "630 Beer Underpass Suite 372", 
      "street_name" : "87672 Lind Burg", 
      "suburb" : "West Virginia", 
      "postal_code" : "84356-3662", 
      "state" : "Qatar", 
      "country" : "British Indian Ocean Territory (Chagos Archipelago)" 
     }, 
     "shipping_address" : { 
      "unit_no" : "6011865161287875", 
      "street_no" : "8292 Langosh Drive Suite 065", 
      "street_name" : "9844 Nicolas Mount", 
      "suburb" : "West Virginia", 
      "postal_code" : "07014", 
      "state" : "Togo", 
      "country" : "Tunisia" 
     }, 
     "job_title" : "Geoscientists", 
     "employer_name" : "Aufderhar Group", 
     "income" : 72577, 
     "phone_numbers" : { 
      "work_phone" : "993-783-7499", 
      "home_phone" : "701.546.7016", 
      "mobile" : "1-346-729-4392" 
     }, 
     "emails" : { 
      "work_emails" : "[email protected]", 
      "personal_emails" : "[email protected]" 
     }, 
     "languages" : "italian", 
     "data_source" : "soldi", 
     "date_added" : ISODate("2017-02-15T10:23:15.000Z"), 
     "source" : "soldi", 
     "is_latest" : "inactive" 
    }, 
    { 
     "persona_fname" : "Julien", 
     "middle_name" : "Hirthe", 
     "persona_lname" : "Schaefer", 
     "gender" : "female", 
     "date_of_birth" : ISODate("1987-06-16T19:00:00.000Z"), 
     "marital_status" : "single", 
     "height" : "2.1 feet", 
     "weight" : "5 kg", 
     "measurement" : { 
      "waist" : "34 inch" 
     }, 
     "disabilities" : "No", 
     "race" : "Asian", 
     "nationality" : "Sudan", 
     "life_cycle" : "single", 
     "children" : { 
      "gender" : "female", 
      "date_of_birth" : ISODate("1987-06-16T19:00:00.000Z") 
     }, 
     "medicare_no" : "4024007131689860", 
     "driving_licence_no" : "OGOSUOIFSDN", 
     "id_no" : "5523297913341227", 
     "passport_no" : "WWZYTE489ZR", 
     "residential_address" : { 
      "unit_no" : 603, 
      "street_no" : "338 Mueller Gardens Suite 397", 
      "street_name" : "6302 Catalina Isle", 
      "suburb" : "West Virginia", 
      "postal_code" : "97534", 
      "state" : "Antarctica (the territory South of 60 deg S)", 
      "country" : "Turks and Caicos Islands" 
     }, 
     "work_address" : { 
      "unit_no" : "6011865161287875", 
      "street_no" : "8561 Jesus Ridges Apt. 662", 
      "street_name" : "1869 Josiah Wall Apt. 347", 
      "suburb" : "West Virginia", 
      "postal_code" : "14810", 
      "state" : "Zambia", 
      "country" : "Mongolia" 
     }, 
     "shipping_address" : { 
      "unit_no" : "6011865161287875", 
      "street_no" : "514 Heller Center", 
      "street_name" : "835 Paxton Cliffs Suite 040", 
      "suburb" : "West Virginia", 
      "postal_code" : "48942-3845", 
      "state" : "Palau", 
      "country" : "Belize" 
     }, 
     "job_title" : "Geoscientists", 
     "employer_name" : "Aufderhar Group", 
     "income" : 72577, 
     "phone_numbers" : { 
      "work_phone" : "(553) 892-7614 x573", 
      "home_phone" : "796.308.3001 x88799", 
      "mobile" : "+1-876-339-1755" 
     }, 
     "emails" : { 
      "work_emails" : "[email protected]", 
      "personal_emails" : "[email protected]" 
     }, 
     "languages" : "german", 
     "data_source" : "soldi", 
     "date_added" : ISODate("2017-02-15T10:23:15.000Z"), 
     "source" : "soldi", 
     "is_latest" : "active" 
    } 
] 
    } 

回答

1

在您的組管道中,c將年齡範圍作爲_id密鑰的一部分並且這可以通過使用$concat"$cond"運營商來完成。考慮運行下面的管道,以獲得期望的結果:

db.amplifyindex.aggregate([ 
    { "$unwind": "$demographic" }, 
    { "$match": { "demographic.is_latest": "active" } }, 
    { 
     "$project": { 
      "age": { 
       "$divide": [ 
        { 
         "$subtract": [ 
          new Date(), 
          { "$ifNull": ["$demographic.date_of_birth", new Date()] } 
         ] 
        }, 
        1000 * 86400 * 365 
       ] 
      } 
     } 
    }, 
    { 
     "$group": { 
      "_id": { 
       "$concat": [ 
        { "$cond": [ { "$lte": [ "$age", 0 ] }, "Unknown", ""] }, 
        { "$cond": [ { "$and": [ { "$gt": ["$age", 0 ] }, { "$lt": ["$age", 10] } ]}, "Under 10", ""] }, 
        { "$cond": [ { "$and": [ { "$gte": ["$age", 10] }, { "$lt": ["$age", 31] } ]}, "10 - 30", ""] }, 
        { "$cond": [ { "$and": [ { "$gte": ["$age", 31] }, { "$lt": ["$age", 51] } ]}, "31 - 50", ""] }, 
        { "$cond": [ { "$and": [ { "$gte": ["$age", 51] }, { "$lt": ["$age", 71] } ]}, "51 - 70", ""] }, 
        { "$cond": [ { "$gte": [ "$age", 71 ] }, "Over 70", ""] } 
       ] 
      }, 
      "personas": { "$sum": 1 } 
     } 
    }, 
    { "$project": { "_id": 0, "age": "$_id", "personas": 1 } } 
]) 

在上面的管道,我已經採取了對空日期字段的查詢,並與$project$ifNull運營商取代了它管道。這將返回生日字段爲空的當前日期,從而生成0歲的年齡,這將落入特殊的年齡段範圍,因此需要包括所有文檔,包括出生日期字段缺失的日期。

在該$concat運營商用於所有$cond表現的情況下,該組合是至關重要的,因爲它作爲一個case語句另有會有嵌套$cond運營商。

爲了理解這個設計,從第三$cond表達

{ 
    "$cond": [ 
     { 
      "$and": [ 
       { "$gte": ["$age", 10] }, 
       { "$lt": ["$age", 31] } 
      ] 
     }, 
     "10 - 30", // matching if 
     "" // else 
    ] 
}, 

其基本上表示所述邏輯

if ("$age" >= 10 && "$age" < 31) { return "10 - 30"; } 
else return ""; 

$concat$cond運營商爲使用時是非常有用的表達式將返回一個空的VA除了匹配的一個,這反過來會返回年齡組。

例如,30.645411時代將返回

"$concat": [ "", "", "10 - 30", "", "", "" ] 

,然後讓你用"10 - 30".

+1

價值_id非常感謝你。完美的幫助。 @chridam –

+0

你也可以通過使用[bucket](https://docs.mongodb.com/manual/reference/operator/aggregation/bucket/)及其邊界來改善你的查詢 –