首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MongoDB:如何更改_id_索引的引擎类型(从B树到LSM树)?

MongoDB:如何更改_id_索引的引擎类型(从B树到LSM树)?
EN

Stack Overflow用户
提问于 2020-01-15 20:11:02
回答 1查看 894关注 0票数 1

我们可以使用WiredTiger engine和type=lsm创建一个集合,但是在MongoDB文档中没有提到这个特性:

代码语言:javascript
复制
db.createCollection(
    "test",
    { storageEngine: { wiredTiger: {configString: "type=lsm"}}}
)

一旦插入了一些文档并添加了索引,看起来WiredTiger真的创建了LSM文件。

代码语言:javascript
复制
db.test.insert([
    { value: 1},
    { value: 2},
    { value: 3}
]) // Done in 16:04
db.test.createIndex(
    { value: 1 },
    { storageEngine: { wiredTiger: {configString: "type=lsm"}}}
) // Done in 19:59
代码语言:javascript
复制
$ ls -ltr
...
-rw-r--r--. 1 mongod mongod        16384 Jan 15 16:04 collection-0-1708338433081558809-000002.lsm
-rw-r--r--. 1 mongod mongod        16384 Jan 15 16:04 index-1-1708338433081558809.wt
-rw-r--r--. 1 mongod mongod        16384 Jan 15 19:59 index-3-1708338433081558809-000002.lsm

集合和索引树看起来像LSM- value_1,但是索引_id_看起来仍然像B-树。

如何更改索引_id的引擎类型

EN

回答 1

Stack Overflow用户

发布于 2020-01-15 21:16:21

这不是你想听到的答案,但目前是不可能的。

_id是一种比较特殊的索引。来自https://github.com/mongodb/mongo/blob/73b456d5c059b17d1c7f0f8badb7c72391ee2173/src/mongo/db/catalog/index_key_validate.cpp#L74

所有索引的规范验证器:

代码语言:javascript
复制
static std::set<StringData> allowedFieldNames = {
    IndexDescriptor::k2dIndexBitsFieldName,
    IndexDescriptor::k2dIndexMaxFieldName,
    IndexDescriptor::k2dIndexMinFieldName,
    IndexDescriptor::k2dsphereCoarsestIndexedLevel,
    IndexDescriptor::k2dsphereFinestIndexedLevel,
    IndexDescriptor::k2dsphereVersionFieldName,
    IndexDescriptor::kBackgroundFieldName,
    IndexDescriptor::kCollationFieldName,
    IndexDescriptor::kDefaultLanguageFieldName,
    IndexDescriptor::kDropDuplicatesFieldName,
    IndexDescriptor::kExpireAfterSecondsFieldName,
    IndexDescriptor::kGeoHaystackBucketSize,
    IndexDescriptor::kIndexNameFieldName,
    IndexDescriptor::kIndexVersionFieldName,
    IndexDescriptor::kKeyPatternFieldName,
    IndexDescriptor::kLanguageOverrideFieldName,
    IndexDescriptor::kNamespaceFieldName,
    IndexDescriptor::kPartialFilterExprFieldName,
    IndexDescriptor::kPathProjectionFieldName,
    IndexDescriptor::kSparseFieldName,
    IndexDescriptor::kStorageEngineFieldName,
    IndexDescriptor::kTextVersionFieldName,
    IndexDescriptor::kUniqueFieldName,
    IndexDescriptor::kWeightsFieldName,
    // Index creation under legacy writeMode can result in an index spec with an _id field.
    "_id"};

_id索引允许的规范:

代码语言:javascript
复制
static const std::set<StringData> allowedIdIndexFieldNames = {
    IndexDescriptor::kCollationFieldName,
    IndexDescriptor::kIndexNameFieldName,
    IndexDescriptor::kIndexVersionFieldName,
    IndexDescriptor::kKeyPatternFieldName,
    IndexDescriptor::kNamespaceFieldName,
    // Index creation under legacy writeMode can result in an index spec with an _id field.
    "_id"};

如您所见,除了名称、排序规则、版本等之外,您可以灵活地更改任何内容。其中没有kStorageEngineFieldName

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59751187

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档