Mongodb- နယ်ပယ်များစွာဖြင့် အုပ်စုဖွဲ့နည်း


နယ်ပယ်များစွာကို အုပ်စုဖွဲ့ပြီး MongoDB တွင် စုစည်းမှုလုပ်ဆောင်ရန် အောက်ပါ syntax ကို သင်အသုံးပြုနိုင်သည်-

 db.collection.aggregate([
    { $group : { _id :{field1:" $field1 ", field2:" $field2 "}, count :{ $sum :1}}}
])

အောက်ဖော်ပြပါ ဥပမာများသည် အောက်ပါစာရွက်စာတမ်းများပါရှိသော စုစည်းမှု အဖွဲ့ နှင့်အတူ ဤအထားအသိုကို အသုံးပြုနည်းကို ပြသသည်-

 db.teams.insertOne({team: " Mavs ", position: " Guard ", points: 31 })
db.teams.insertOne({team: " Mavs ", position: " Guard ", points: 22 })
db.teams.insertOne({team: " Mavs ", position: " Forward ", points: 19 })
db.teams.insertOne({team: " Rockets ", position: " Guard ", points: 26 })
db.teams.insertOne({team: " Rockets ", position: " Forward ", points: 33 })

ဥပမာ 1- အကွက်များစွာဖြင့် အုပ်စုဖွဲ့ပြီး စုစည်းပါ။

“ အဖွဲ့” နှင့် “ ရာထူး” ဖြင့် အုပ်စုဖွဲ့ရန် အောက်ပါကုဒ်ကို သုံးနိုင်ပြီး အုပ်စုတစ်ခုစီ၏ ဖြစ်ပျက်မှုများကို ရေတွက်နိုင်သည်-

 db.teams.aggregate([
    { $group : { _id : {team: " $team ", position: " $position "}, count :{ $sum :1}}}
])

၎င်းသည် အောက်ပါရလဒ်များကို ပြန်ပေးသည်-

 { _id: { team: ' Rockets ', position: ' Forward ' }, count: 1 }
{ _id: { team: ' Mavs ', position: ' Guard ' }, count: 2 }
{ _id: { team: ' Mavs ', position: ' Forward ' }, count: 1 }
{ _id: { team: ' Rockets ', position: ' Guard ' }, count: 1 }

မတူညီတဲ့ စုစည်းမှုတစ်ခုကိုလည်း လုပ်နိုင်ပါတယ်။ ဥပမာအားဖြင့်၊ ကျွန်ုပ်တို့သည် “အဖွဲ့” နှင့် ရာထူး” ဖြင့် အုပ်စုဖွဲ့နိုင်ပြီး အုပ်စုဖွဲ့ခြင်းဖြင့် “အမှတ်များ” ၏ပေါင်းလဒ်ကို ရှာဖွေနိုင်သည်-

 db.teams.aggregate([
    { $group : { _id : {team: " $team ", position: " $position "}, sumPoints :{ $sum : " $points "}}}
])

၎င်းသည် အောက်ပါရလဒ်များကို ပြန်ပေးသည်-

 { _id: { team: ' Rockets ', position: ' Forward ' }, sumPoints: 33 }
{ _id: { team: ' Mavs ', position: ' Guard ' }, sumPoints: 53 }
{ _id: { team: ' Mavs ', position: ' Forward ' }, sumPoints: 19 }
{ _id: { team: ' Rockets ', position: ' Guard ' }, sumPoints: 26 }

၎င်းသည် ကျွန်ုပ်တို့အား ပြောပြသည်-

  • ‘Forward’ အနေအထားတွင် ‘Rockets’ ကစားသမားများက ရမှတ်ပေါင်း 33 မှတ်ဖြစ်သည်။
  • ‘Guard’ အနေအထားတွင် ‘Mavs’ ကစားသမားများက ရမှတ်ပေါင်း 53 မှတ်ဖြစ်သည်။

နောက် … ပြီးတော့။

ဥပမာ 2- အကွက်များစွာဖြင့် အုပ်စုဖွဲ့ပြီး အစုလိုက် (ထို့နောက် စီပါ)

“ အဖွဲ့” နှင့် ရာထူးအလိုက် အုပ်စုဖွဲ့ရန် အောက်ပါကုဒ်ကို သုံးနိုင်ပြီး အုပ်စုဖွဲ့ခြင်းဖြင့် “ အမှတ်” ၏ ပေါင်းလဒ်ကို ရှာဖွေနိုင်ပြီး ရလဒ်များကို “ အမှတ်” မှ ငယ်စဉ်ကြီးလိုက် ဖြင့် စီပါ။

 db.teams.aggregate([
    { $group : { _id : {team: " $team ", position: " $position "}, sumPoints :{ $sum : " $points "}}},
    { $sort : { sumPoints :1}}
])

၎င်းသည် အောက်ပါရလဒ်များကို ပြန်ပေးသည်-

 { _id: { team: ' Mavs ', position: ' Forward ' }, sumPoints: 19 }
{ _id: { team: ' Rockets ', position: ' Guard ' }, sumPoints: 26 }
{ _id: { team: ' Rockets ', position: ' Forward ' }, sumPoints: 33 }
{ _id: { team: ' Mavs ', position: ' Guard ' }, sumPoints: 53 }

ရလဒ်များကို ကြီးစဉ်ငယ်လိုက် စီရန် -1 ကို သုံးနိုင်သည်။

 db.teams.aggregate([
    { $group : { _id : {team: " $team ", position: " $position "}, sumPoints :{ $sum : " $points "}}},
    { $sort : { sumPoints :-1}}
])

၎င်းသည် အောက်ပါရလဒ်များကို ပြန်ပေးသည်-

 { _id: { team: ' Mavs ', position: ' Guard ' }, sumPoints: 53 }
{ _id: { team: ' Rockets ', position: ' Forward ' }, sumPoints: 33 }
{ _id: { team: ' Rockets ', position: ' Guard ' }, sumPoints: 26 }
{ _id: { team: ' Mavs ', position: ' Forward ' }, sumPoints: 19 }

မှတ်ချက်$group အတွက် စာရွက်စာတမ်းအပြည့်အစုံကို ဤနေရာတွင် ရှာနိုင်သည်။

မှတ်ချက်တစ်ခုထည့်ပါ။

သင့် email လိပ်စာကို ဖော်ပြမည် မဟုတ်ပါ။ လိုအပ်သော ကွက်လပ်များကို * ဖြင့်မှတ်သားထားသည်