Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> MongoDB

การสร้างดัชนีบนฟิลด์ซ้อน (Nested Field) ใน MongoDB

ใช้ Dot Notation ใน createIndex

สามารถสร้างดัชนีบนฟิลด์ซ้อนได้โดยใช้ dot notation ระบุเส้นทางของฟิลด์ เริ่มต้นด้วยการสร้างคอลเลกชันพร้อมข้อมูลผู้ใช้ซ้อนกัน

> db.createIndexOnNestedFieldDemo.insertOne({ "UserDetails": { "UserPersonalDetails": { "UserFirstName": "John", "UserLastName": "Smith" } } });

คำสั่งสร้างดัชนีบน UserLastName:

> db.createIndexOnNestedFieldDemo.createIndex({"UserDetails.UserPersonalDetails.UserLastName": 1});

ผลลัพธ์ยืนยันการสร้างดัชนีสำเร็จ