Thuta Learning
ရှာဖွေရန်
ExercisesData & Databasesintermediate

လေ့ကျင့်ခန်း — Query Operators နှင့် Aggregation Pipeline

စိတ်လျှော့ပါ။ ဒီခန်းကို စာအုပ်လိုမဟုတ်ဘဲ စကားပြောသလိုပဲ၊ နားလည်လွယ်အောင် ရှင်းပါမယ်။

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • လေ့ကျင့်ခန်း — Query Operators နှင့် Aggregation Pipeline ကို ကိုယ်တိုင် လေ့ကျင့်ကြည့်မယ်
  • သင်ခဲ့ပြီးသား skill တွေကို practice လုပ်ပြီး ခိုင်မာအောင်လုပ်မယ်
  • အမှားရှာ၊ ပြင်တတ်၊ ကိုယ်တိုင် check လုပ်တတ်မယ်

ခဏလေး ဒီလိုပဲ စဉ်းစားကြည့်

ဒီလေ့ကျင့်ခန်းက Intermediate နှင့် Advanced chapter မှာ လေ့လာခဲ့တဲ့ query operators, indexes, aggregation framework အသိပညာကို ပေါင်းစပ်ပြီး ပိုခက်ခဲတဲ့ scenario များနဲ့ practice လုပ်ဖို့ ဖြစ်ပါတယ်။ orders collection တစ်ခုကို အခြေခံပြီး comparison/logical operators, index creation, multi-stage aggregation pipeline များကို ကိုယ်တိုင် ရေးဖြေရမည်ဖြစ်သည်။ ဒီလေ့ကျင့်ခန်းအောင်မြင်ရင် MongoDB ရဲ့ core skill အားလုံးကို confidence ရှိရှိ အသုံးချနိုင်ပြီဆိုတာ သက်သေပြနိုင်ပါလိမ့်မယ်။

လေ့ကျင့်ခန်းများ

Task 1: orders collection ကို amount, status, customer, createdAt field ပါသော document ၅ ခု insertMany() ဖြင့် ထည့်ပါ။ Task 2: $or operator ကိုသုံးပြီး status: "pending" ဖြစ်သော် သို့မဟုတ် amount 100 ထက်ကြီးသော order များကို find() ဖြင့် ရှာပါ။ Task 3: customer field ပေါ်မှာ index တစ်ခု createIndex() ဖြင့် တည်ဆောက်ပါ။ Task 4: aggregation pipeline တစ်ခုရေးပြီး status: "completed" ဖြစ်သော order များကို customer အလိုက် စုစည်းကာ (group) customer တစ်ဦးစီ၏ စုစုပေါင်း amount ကို $sum ဖြင့် တွက်ချက်ပြီး amount အများဆုံးမှ အနည်းဆုံးသို့ sort လုပ်ပါ။

Code နမူနာ

javascript
// Task 1: sample orders data insert လုပ်ပါ
db.orders.insertMany([
  { customer: "Zin Mar", amount: 150, status: "completed", createdAt: new Date("2026-08-01") },
  { customer: "Htet Htet", amount: 80, status: "pending", createdAt: new Date("2026-08-05") },
  { customer: "Zin Mar", amount: 220, status: "completed", createdAt: new Date("2026-08-10") },
  { customer: "Kyaw Kyaw", amount: 60, status: "pending", createdAt: new Date("2026-08-12") },
  { customer: "Htet Htet", amount: 300, status: "completed", createdAt: new Date("2026-08-15") }
])

// Task 2: $or operator ဖြင့် query ရေးပါ (ဒီနေရာမှာ ရေးပါ)


// Task 3: customer field ပေါ်မှာ index တည်ဆောက်ပါ (ဒီနေရာမှာ ရေးပါ)


// Task 4: customer အလိုက် completed order amount စုစည်းသော aggregation pipeline ရေးပါ (ဒီနေရာမှာ ရေးပါ)
You should see
Task 4 ပြီးသောအခါ { _id: "Htet Htet", total: 300 }, { _id: "Zin Mar", total: 370 } စသည်ဖြင့် customer တစ်ဦးစီ၏ completed order စုစုပေါင်း amount ကို sort လုပ်ထားသည့် document array ရရှိမည်။

၅ မိနစ် စမ်းကြည့်

Task 4 ရဲ့ pipeline ကို $project stage ထပ်ထည့်ပြီး total field ကိုသာ ပြသအောင် ၅ မိနစ်အတွင်း ပြင်ရေးကြည့်ပါ။

သတိလေးတစ်ချက်

Aggregation pipeline ရှုပ်ထွေးလာလေလေ stage တစ်ခုချင်းစီရဲ့ output ကို explain() သို့မဟုတ် stage အဆင့်လိုက် run ကြည့်ခြင်းဖြင့် debug လုပ်ခြင်းက အထောက်အကူဖြစ်စေသည်။

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • $group stage ၏ $sum expression ထဲမှာ field name ကို "$amount" ဟု $ တပ်ရမည်ကို မေ့ပြီး "amount" ဟု တိုက်ရိုက်ရေးမိတတ်သည်
  • $or operator ရဲ့ condition တွေကို array အဖြစ် [ ] ထဲမှာ မထားဘဲ object အဖြစ်ရေးမိပြီး syntax error တက်တတ်သည်

အခု ကိုယ်တိုင် စမ်းကြည့်

Task 4 ရဲ့ pipeline ကို $project stage ထပ်ထည့်ပြီး total field ကိုသာ ပြသအောင် ၅ မိနစ်အတွင်း ပြင်ရေးကြည့်ပါ။

You'll know it worked when: Task 4 ပြီးသောအခါ { _id: "Htet Htet", total: 300 }, { _id: "Zin Mar", total: 370 } စသည်ဖြင့် customer တစ်ဦးစီ၏ completed order စုစုပေါင်း amount ကို sort လုပ်ထားသည့် document array ရရှိမည်။

လေ့ကျင့်ခန်း — Query Operators နှင့် Aggregation Pipeline | Thuta Learning