首页 > 解决方案 > MongoDB: How can I change engine type (from B-Tree to LSM-Tree) of _id_ index?

问题描述

We can create a collection with WiredTiger engine and type=lsm, but this feature is not mentioned in MongoDB documents:

db.createCollection(
    "test",
    { storageEngine: { wiredTiger: {configString: "type=lsm"}}}
)

Once insert some documents and add an index, it seems WiredTiger really creates LSM files.

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
$ 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

Collection and index value_1 seem like LSM-Tree, but index _id_ still seems like B-Tree.

How can I change engine type of index _id?

标签: mongodbindexingprimary-keywiredtigerlsm-tree

解决方案


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

_id 是一个非常特殊的索引。从https://github.com/mongodb/mongo/blob/73b456d5c059b17d1c7f0f8badb7c72391ee2173/src/mongo/db/catalog/index_key_validate.cpp#L74

所有索引的规范验证器:

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 索引允许的规范:

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


推荐阅读