mongodb-嵌入式字段与嵌入式文档索引

时间:2018-10-18 04:53:15

标签: mongodb indexing

所以我想知道是否要在文档上创建索引,它是否还会在嵌入字段上固有地创建索引?

因此,示例:

{
  name: {
          first: "Yukihiro",
          last: "Matsumoto"
        }
}

如果它是嵌入式文档索引,将使用以下内容执行搜索:

   {
     "name.first": "Yukihiro",
     "name.last": "Matsumoto"
   }

导致它使用索引进行搜索,还是要查找O(n)文档?

1 个答案:

答案 0 :(得分:3)

是的,只要文档的大小不超过允许的索引大小-https://docs.mongodb.com/manual/core/index-single/#create-an-index-on-embedded-document

您可以检查特定查询是否在使用带有查询功能的索引。 "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\"]"
                        ]
                }
        }
}
相关问题