Thuta Learning
MongoDB
ExercisesData & Databasesအလယ်အလတ်

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

MongoDBသင်ခန်းစာ 23

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

  • လေ့ကျင့်ခန်း — 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 ကိုသာ ပြသအောင် ၅ မိနစ်အတွင်း ပြင်ရေးကြည့်ပါ။

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

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

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