时间:2021-07-01 10:21:17 帮助过:17人阅读
"keyPattern" : {
"info" : 1
},
"indexName" : "info_1",
"isMultiKey" : false,
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 1,
"direction" : "forward",
"indexBounds" : {
"info" : [
"[{ city: \"beijing\" }, { city: \"beijing\" }]"
]
}
}
},
"rejectedPlans" : [ ]
},
"serverInfo" : {
"host" : "mycentos.WORKGROUP",
"port" : 27017,
"version" : "3.2.8",
"gitVersion" : "ed70e33130c977bda0024c125b56d159573dbaf0"
},
"ok" : 1
}
但是这样的查询就不行:
>db.data.find({"info.city":"beijing"}); //字段部分必须加引号
>db.data.find({info.url:"..."});
这样的查询语句,只能使用类似的组合索引:
> db.data.ensureIndex({"info.url":1, "info.city":1});
5.组合索引
> db.data.ensureIndex({"info.url":1, "info.city":1});
即使查询时,与定义的排序相反,也是可以使用索引扫描的。
rs0:PRIMARY> db.data.find({"info.url": /http:*/i}).sort({"info.url": -1, "info.city":-1}).explain()
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "test.data",
"indexFilterSet" : false,
"parsedQuery" : {
"info.url" : /http:*/i
},
"winningPlan" : {
"stage" : "FETCH",
"inputStage" : {
"stage" : "IXSCAN",
"filter" : {
"info.url" : /http:*/i
},
"keyPattern" : {
"info.url" : 1,
"info.city" : 1
},
"indexName" : "info.url_1_info.city_1",
"isMultiKey" : false,
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 1,
"direction" : "backward",
"indexBounds" : {
"info.url" : [
"[/http:*/i, /http:*/i]",
"({}, \"\"]"
],
"info.city" : [
"[MaxKey, MinKey]"
]
}
}
},
"rejectedPlans" : [ ]
},
"serverInfo" : {
"host" : "mycentos.WORKGROUP",
"port" : 27017,
"version" : "3.2.8",
"gitVersion" : "ed70e33130c977bda0024c125b56d159573dbaf0"
},
"ok" : 1
}
mongodb中在嵌套子文档的文档上面建立索引
标签:mongodb