Mongodb- အကွက်မျိုးစုံမှ ကွဲပြားသောတန်ဖိုးများကို ရွေးချယ်နည်း


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

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

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

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

ဥပမာ- အကွက်များစွာတွင် ကွဲပြားသောတန်ဖိုးများကို ရွေးပါ။

ကွဲပြားသောအဖွဲ့နှင့် နေရာကွက်လပ်တန်ဖိုးများအားလုံးကို ရှာဖွေရန် အောက်ပါမေးခွန်းကို ကျွန်ုပ်တို့ အသုံးပြုနိုင်ပါသည်။

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

ဤမေးမြန်းချက်သည် အောက်ပါစာရွက်စာတမ်းများကို ပြန်ပေးသည်-

 { _id: { team: 'Mavs', position: 'Guard' } }
{ _id: { team: 'Rockets', position: 'Forward' } }
{ _id: { team: 'Rockets', position: 'Center' } }
{ _id: { team: 'Cavs', position: 'Guard' } }

အသင်း၊ ရာထူးနှင့် အမှတ်အကွက်များ၏ ကွဲပြားသောတန်ဖိုးအားလုံးကို ရှာဖွေရန် အောက်ပါမေးခွန်းကို ကျွန်ုပ်တို့ အသုံးပြုနိုင်သည်။

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

ဤမေးမြန်းချက်သည် အောက်ပါစာရွက်စာတမ်းများကို ပြန်ပေးသည်-

 { _id: { team: 'Cavs', position: 'Guard', points: 33 } }
{ _id: { team: 'Rockets', position: 'Forward', points: 29 } }
{ _id: { team: 'Mavs', position: 'Guard', points: 22 } }
{ _id: { team: 'Rockets', position: 'Forward', points: 26 } }
{ _id: { team: 'Mavs', position: 'Guard', points: 31 } }
{ _id: { team: 'Rockets', position: 'Center', points: 19 } }

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

ထပ်လောင်းအရင်းအမြစ်များ

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

MongoDB- အစုအဝေးတစ်ခုသို့ အကွက်အသစ်တစ်ခုထည့်နည်း
MongoDB- အုပ်စုဖွဲ့ပြီး ရေတွက်နည်း
MongoDB- နယ်ပယ်များစွာဖြင့် အုပ်စုဖွဲ့နည်း

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

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