【发布时间】:2021-07-04 12:48:28
【问题描述】:
我在测试 MongoDB 中的索引是如何工作的,一时搞不懂。
我还创建了新索引:
collection.createIndex({ SomeValue1: 1, SomeValue2: -1 })
而且我不明白索引如何将自己的条目映射到原始表。
我认为它使用了“_id”列,但是这两个查询告诉我这不是真的:
#1:
collection
.find()
.sort({ SomeValue1: 1, SomeValue2: -1 })
.projection({ SomeValue1: 1, SomeValue2: 1 })
.explain("allPlansExecution")
#2:
collection
.find()
.sort({ SomeValue1: 1, SomeValue2: -1 })
.projection({ SomeValue1: 1, SomeValue2: 1, _id: 0 })
.explain("allPlansExecution")
在 #1 查询执行计划说:
"totalKeysExamined": 10000,
"totalDocsExamined": 10000,
在#2:
"totalKeysExamined": 10000,
"totalDocsExamined": 0,
我原以为这两种变体都不使用原始表格。
在官方文档中找到此信息:link
但是如果它(索引表)不使用'_id'字段,仍然不明白如何索引条目匹配原始表中的条目?
【问题讨论】:
标签: mongodb indexing mongodb-query