สำหรับข้อมูลเกี่ยวกับแผนแบบสอบถาม ใช้อธิบาย () ใน MongoDB ให้เราสร้างคอลเลกชันที่มีเอกสาร -
> db.demo637.ensureIndex({ClientName:1}); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } > db.demo637.insert({ClientName:"John"}); WriteResult({ "nInserted" : 1 }) > db.demo637.insert({ClientName:"Bob"}); WriteResult({ "nInserted" : 1 }) > db.demo637.insert({ClientName:"Johnson"}); WriteResult({ "nInserted" : 1 })
แสดงเอกสารทั้งหมดจากคอลเล็กชันโดยใช้วิธี find() -
> db.demo637.find();
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -
{ "_id" : ObjectId("5e9c26916c954c74be91e6db"), "ClientName" : "John" } { "_id" : ObjectId("5e9c26936c954c74be91e6dc"), "ClientName" : "Bob" } { "_id" : ObjectId("5e9c269d6c954c74be91e6dd"), "ClientName" : "Johnson" }
กรณีที่ 1 −
นี่คือข้อความค้นหาสำหรับ regexp / / −
> db.demo637.find({ClientName:/john/}).explain();
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -
{ "queryPlanner" : { "plannerVersion" : 1, "namespace" : "test.demo637", "indexFilterSet" : false, "parsedQuery" : { "ClientName" : { "$regex" : "john" } }, "winningPlan" : { "stage" : "FETCH", "inputStage" : { "stage" : "IXSCAN", "filter" : { "ClientName" : { "$regex" : "john" } }, "keyPattern" : { "ClientName" : 1 }, "indexName" : "ClientName_1", "isMultiKey" : false, "multiKeyPaths" : { "ClientName" : [ ] }, "isUnique" : false, "isSparse" : false, "isPartial" : false, "indexVersion" : 2, "direction" : "forward", "indexBounds" : { "ClientName" : [ "[\"\", {})", "[/john/, /john/]" ] } } }, "rejectedPlans" : [ ] }, "serverInfo" : { "host" : "DESKTOP-QN2RB3H", "port" : 27017, "version" : "4.0.5", "gitVersion" : "3739429dd92b92d1b0ab120911a23d50bf03c412" }, "ok" : 1 }
กรณีที่ 2 −
ต่อไปนี้เป็นแบบสอบถามสำหรับ regexp /^ / −
> db.demo637.find({ClientName:/^john/}).explain();
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -
{ "queryPlanner" : { "plannerVersion" : 1, "namespace" : "test.demo637", "indexFilterSet" : false, "parsedQuery" : { "ClientName" : { "$regex" : "^john" } }, "winningPlan" : { "stage" : "FETCH", "inputStage" : { "stage" : "IXSCAN", "keyPattern" : { "ClientName" : 1 }, "indexName" : "ClientName_1", "isMultiKey" : false, "multiKeyPaths" : { "ClientName" : [ ] }, "isUnique" : false, "isSparse" : false, "isPartial" : false, "indexVersion" : 2, "direction" : "forward", "indexBounds" : { "ClientName" : [ "[\"john\", \"joho\")", "[/^john/, /^john/]" ] } } }, "rejectedPlans" : [ ] }, "serverInfo" : { "host" : "DESKTOP-QN2RB3H", "port" : 27017, "version" : "4.0.5", "gitVersion" : "3739429dd92b92d1b0ab120911a23d50bf03c412" }, "ok" : 1 }