mongodb-dba-workshop

Single Field Indexes with Sub-document

Exercise

0. เตรียมระบบ (ถ้ายังไม่ได้ทำ)

สร้าง mongodb cluster บน mongoDB Atlas และเชื่อมต่อกับ Mongo Cluster

1. ทดสอบ Single Fields Index แบบ sub-document

  1. จาก database sample_mflix ให้เลือก movies collection มาใช้ทดสอบ
  2. ในช่อง Query ให้ใส่คำสั่งดังนี้ และกดปุ่ม Find
{ "tomatoes.viewer.meter": { "$gt": 90 } }
  1. ตรวจสอบผลลัพธ์ที่ได้ จะเป็นหนังที่มีคะแนนจากผู้ชมมากกว่า 90
  2. กดปุ่ม Explain และสังเกตค่าการทำงานต่อไปนี้
    1. Plan
    2. Document examined
    3. Document returned
    4. Execution Time
  3. กดปิดหน้าต่าง explain และกลับมาที่หน้าจอเดิม
  4. กด tab Indexes และสังเกตว่ามี index อะไรบ้าง
  5. กดปุ่ม Create Index และสร้าง index ใหม่ โดยใช้ field tomatoes.viewer.meter และเลือกเป็น ascending (1)
  6. กลับมาที่ tab documents ให้ทำการเลือก Query ใหม่อีกครั้ง
{ "tomatoes.viewer.meter": { "$gt": 90 } }
  1. กดปุ่ม find และตรวจสอบผลลัพธ์ที่ได้
  2. กดปุ่ม Explain และสังเกตค่าการทำงานต่อไปนี้
  3. Plan
  4. Document examined
  5. Document returned
  6. Execution Time
  7. กลับมาที่ tab Documents และรัน Query ดังนี้
{
  "tomatoes.viewer.meter": { "$gt": 75 },
  "genres": "Drama"
}
  1. ตรวจสอบผลลัพธ์ที่ได้ จะเป็นหนังที่มีคะแนนจากผู้ชมมากกว่า 75 และเป็นหนังแนว Drama
    1. กดปุ่ม Explain และสังเกตค่าการทำงานต่อไปนี้
  2. Plan (เป็น index scan ก่อนที่จะ filter โดยใช้ genres)
  3. Document examined
  4. Document returned
  5. Execution Time
  6. กลับไปที่ tab indexes และลบ index tomatoes.viewer.meter ออก