我创建了一个复合索引:
db.installedbase.createIndex({serviceline_id:1, product:"text"})
但是发现在点击find()
db.installedbase.find({serviceline_id:601.0}).explain()
输出:
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "entitlements.installedbase",
"indexFilterSet" : false,
"parsedQuery" : {
"serviceline_id" : {
"$eq" : 601.0
}
},
"winningPlan" : {
"stage" : "COLLSCAN",
"filter" : {
"serviceline_id" : {
"$eq" : 601.0
}
},
"direction" : "forward"
},
"rejectedPlans" : []
},
"serverInfo" : {
"host" : "CHNMCT136701L",
"port" : 27017,
"version" : "3.6.3",
"gitVersion" : "9586e557d54ef70f9ca4b43c26892cd55257e1a5"
},
"ok" : 1.0
}
文字搜索:
db.installedbase.find({product:"network_router"}).explain()
输出:
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "entitlements.installedbase",
"indexFilterSet" : false,
"parsedQuery" : {
"product" : {
"$eq" : "network_router"
}
},
"winningPlan" : {
"stage" : "COLLSCAN",
"filter" : {
"product" : {
"$eq" : "network_router"
}
},
"direction" : "forward"
},
"rejectedPlans" : []
},
"serverInfo" : {
"host" : "CHNMCT136701L",
"port" : 27017,
"version" : "3.6.3",
"gitVersion" : "9586e557d54ef70f9ca4b43c26892cd55257e1a5"
},
"ok" : 1.0
}
预期索引将按照此处的文档使用:compound index,但文本索引除外,它仅可用于文本搜索$text
。纠正我,如果我错了,如果有人可以澄清为什么索引不用于我的情况,我将非常感激。
示例数据:
{
"_id" : 801.0,
"serviceline_id" : 601.0,
"product" : "network_router"
}
答案 0 :(得分:1)
因为你不能使用这样的索引。
您必须创建两个独立的索引而不是此索引。
复合指数
复合索引可以包含文本索引键 与升序/降序索引键组合。但是,这些 复合索引具有以下限制:
复合文本索引不能包含任何其他特殊索引类型, 例如多键或地理空间索引字段。如果是复合文字 index包括文本索引键之前的键,以执行$ text 搜索时,查询谓词必须包含相等匹配条件 前面的键。创建复合文本索引时,所有文本 索引键必须在索引规范中相邻列出 文档。
https://docs.mongodb.com/manual/core/index-text/#compound-index