所以,我想知道,如果我要在文档上创建一个索引,那么它是否也内在地为嵌入式字段创建了一个索引?
所以举个例子:
{
name: {
first: "Yukihiro",
last: "Matsumoto"
}
}如果是嵌入的文档索引,将使用以下内容执行搜索:
{
"name.first": "Yukihiro",
"name.last": "Matsumoto"
}让它使用索引进行搜索,还是会查找文档O(n)?
发布于 2018-10-18 05:28:08
是的,只要文档不大于允许的索引大小- https://docs.mongodb.com/manual/core/index-single/#create-an-index-on-embedded-document
您可以检查特定的查询是否使用了查询上带有“explain”函数的索引。"stage": "IXSCAN"表示它使用了一个索引。
"winningPlan" : {
"stage" : "FETCH",
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"type" : 1
},
"indexName" : "type_1",
"isMultiKey" : false,
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 1,
"direction" : "forward",
"indexBounds" : {
"type" : [
"[\"teacher\", \"teacher\"]"
]
}
}
}https://stackoverflow.com/questions/52867233
复制相似问题