auntyellow
auntyellow

Reputation: 2573

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?

Upvotes: 2

Views: 2623

Answers (1)

Alex Blex
Alex Blex

Reputation: 37048

Not an answer you would like to hear but it is not possible at the moment.

_id is quite special index. From https://github.com/mongodb/mongo/blob/73b456d5c059b17d1c7f0f8badb7c72391ee2173/src/mongo/db/catalog/index_key_validate.cpp#L74:

Specification validator for all indexes:

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"};

Specification allowed for _id index:

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"};

As you see you have flexibility to change nothing but name, collation, version etc. There is no kStorageEngineFieldName there.

Upvotes: 5

Related Questions