Home > database >  Remove outdated comound indexes that change often (increment)
Remove outdated comound indexes that change often (increment)

Time:10-28

I have a user document with structure like this

email: string
version: number
username: string

And this user collection has following indexes

collection.createIndex({email: 1}, {unique: true})
collection.createIndex({email: 1, version: 1})

First index ensures that email is unique and second one allows for more efficient lookup and updates when queries like this one are executed

collection.updateOne({email: "...", version: 2}, data)

My concern is this - every time user document is updated it's version is incremented. Does this mean that there is now an index for each combination of email-version? Which means that with time such index will occupy a lot of resources. Is it possible to make mongo remove old email-version index once version changes (as it can only be incremented)?

CodePudding user response:

Indexes are implemented on the storage engine side. Mongo just uses them. In case of wiredtiger, it's b-tree for regular indexes. Any document update which changes indexed field results with deletion of previous node in the tree and insertion of the new one.

  • Related