Single Field Indexes with Sub-document
Exercise
0. เตรียมระบบ (ถ้ายังไม่ได้ทำ)
สร้าง mongodb cluster บน mongoDB Atlas และเชื่อมต่อกับ Mongo Cluster
1. ทดสอบ Single Fields Index แบบ sub-document
- จาก database sample_mflix ให้เลือก movies collection มาใช้ทดสอบ
- ในช่อง Query ให้ใส่คำสั่งดังนี้ และกดปุ่ม Find
{ "tomatoes.viewer.meter": { "$gt": 90 } }
- ตรวจสอบผลลัพธ์ที่ได้ จะเป็นหนังที่มีคะแนนจากผู้ชมมากกว่า 90
- กดปุ่ม Explain และสังเกตค่าการทำงานต่อไปนี้
- Plan
- Document examined
- Document returned
- Execution Time
- กดปิดหน้าต่าง explain และกลับมาที่หน้าจอเดิม
- กด tab Indexes และสังเกตว่ามี index อะไรบ้าง
- กดปุ่ม Create Index และสร้าง index ใหม่ โดยใช้ field tomatoes.viewer.meter และเลือกเป็น ascending (1)
- กลับมาที่ tab documents ให้ทำการเลือก Query ใหม่อีกครั้ง
{ "tomatoes.viewer.meter": { "$gt": 90 } }
- กดปุ่ม find และตรวจสอบผลลัพธ์ที่ได้
- กดปุ่ม Explain และสังเกตค่าการทำงานต่อไปนี้
- Plan
- Document examined
- Document returned
- Execution Time
- กลับมาที่ tab Documents และรัน Query ดังนี้
{
"tomatoes.viewer.meter": { "$gt": 75 },
"genres": "Drama"
}
- ตรวจสอบผลลัพธ์ที่ได้ จะเป็นหนังที่มีคะแนนจากผู้ชมมากกว่า 75 และเป็นหนังแนว Drama
-
- กดปุ่ม Explain และสังเกตค่าการทำงานต่อไปนี้
- Plan (เป็น index scan ก่อนที่จะ filter โดยใช้ genres)
- Document examined
- Document returned
- Execution Time
- กลับไปที่ tab indexes และลบ index tomatoes.viewer.meter ออก