ใช่ คุณสามารถทำได้ ขั้นแรก คุณต้องสร้างดัชนีแล้วใช้อธิบาย () ให้เราสร้างดัชนี MongoDB ก่อน ต่อไปนี้เป็นแบบสอบถาม:
> db.indexOrQueryDemo.ensureIndex({"First":1}); สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 2,
"numIndexesAfter" : 3,
"ok" : 1
} แบบสอบถามเพื่อสร้างดัชนีที่สองมีดังนี้
> db.indexOrQueryDemo.ensureIndex({"Second":1}); สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 3,
"numIndexesAfter" : 4,
"ok" : 1
} ต่อไปนี้เป็นแบบสอบถามสำหรับตัวดำเนินการ $หรือดัชนี เราได้ใช้คำอธิบาย () ที่นี่ด้วย
> db.indexOrQueryDemo.find({$or:[{First:1}, {Second:2}]}).explain(); สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "test.indexOrQueryDemo",
"indexFilterSet" : false,
"parsedQuery" : {
"$or" : [
{
"First" : {
"$eq" : 1
}
},
{
"Second" : {
"$eq" : 2
}
}
]
},
"winningPlan" : {
"stage" : "SUBPLAN",
"inputStage" : {
"stage" : "FETCH",
"inputStage" : {
"stage" : "OR",
"inputStages" : [
{
"stage" : "IXSCAN",
"keyPattern" : {
"First" : 1
},
"indexName" : "First_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"First" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"First" : [
"[1.0, 1.0]"
]
}
},
{
"stage" : "IXSCAN",
"keyPattern" : {
"Second" : 1
},
"indexName" : "Second_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"Second" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"Second" : [
"[2.0, 2.0]"
]
}
}
]
}
}
},
"rejectedPlans" : [ ]
},
"serverInfo" : {
"host" : "DESKTOP-QN2RB3H",
"port" : 27017,
"version" : "4.0.5",
"gitVersion" : "3739429dd92b92d1b0ab120911a23d50bf03c412"
},
"ok" : 1
}