How to update the TTL on a collection?
Asked Answered
M

3

7

Using NodeJS + MongoJS, I have connection to a mongo DB.

I set a TTL on a collection with:

myCollection.createIndex({createdAt: 1}, {expireAfterSeconds: 60 * 30})

Is it now possible to update the value for expireAfterSeconds ? If so, what's the policy for already existing items in the collection, ie : are their TTL updated automatically or are they left untouched ?

Mayemayeda answered 9/3, 2015 at 10:35 Comment(1)
docs.mongodb.com/v3.0/tutorial/modify-an-indexAssignable
A
6

You cannot actually "update" an index definition. What you need to here is "delete" the index and then "re-create" it. So use .dropIndex() first

myCollection.dropIndex({ "createdAt": 1 },function(err,result) { });

Then recreate with a new interval:

myCollection.ensureIndex(
   { "createdAt": 1 },
   { "expireAfterSeconds": 60 * 10 },
   function(err,result) { }
);

As for when it updates, the mongod service runs at an interval every 60 seconds an event you process a delete for any items where the effective date field ( i.e "createdAt" in this example ) is within the "expiry period" from the current time of the server.

So that means it does not matter if you drop the index and re-create it, as the existing server process will just run the same expiry query on each collection that has such an index defined on that interval clock.

Adventurism answered 9/3, 2015 at 10:48 Comment(1)
This answer is incorrect and can cause issues (downtime of no index and performance while rebuilding). You CANm change the index TTL see - docs.mongodb.com/manual/reference/command/collMod/…Sienkiewicz
E
11

Apart of the @Neil answer above, the documentation states, that

You cannot use createIndex() to change the value of expireAfterSeconds of an existing index. Instead use the collMod database command in conjunction with the index collection flag. Otherwise, to change the value of the option of an existing index, you must drop the index first and recreate.

So

db.runCommand({
  "collMod": <collection>,
  "index": {
    keyPattern: <index_spec>,
    expireAfterSeconds: <seconds>
  }
})

should work fine too.

Exhibitionism answered 8/6, 2017 at 7:59 Comment(0)
A
6

You cannot actually "update" an index definition. What you need to here is "delete" the index and then "re-create" it. So use .dropIndex() first

myCollection.dropIndex({ "createdAt": 1 },function(err,result) { });

Then recreate with a new interval:

myCollection.ensureIndex(
   { "createdAt": 1 },
   { "expireAfterSeconds": 60 * 10 },
   function(err,result) { }
);

As for when it updates, the mongod service runs at an interval every 60 seconds an event you process a delete for any items where the effective date field ( i.e "createdAt" in this example ) is within the "expiry period" from the current time of the server.

So that means it does not matter if you drop the index and re-create it, as the existing server process will just run the same expiry query on each collection that has such an index defined on that interval clock.

Adventurism answered 9/3, 2015 at 10:48 Comment(1)
This answer is incorrect and can cause issues (downtime of no index and performance while rebuilding). You CANm change the index TTL see - docs.mongodb.com/manual/reference/command/collMod/…Sienkiewicz
H
3

From: https://docs.mongodb.com/manual/tutorial/expire-data/

You can modify the expireAfterSeconds of an existing TTL index using the collMod command.

See Rob's answer here: https://mcmap.net/q/1476072/-flask-pymongo-collmod

Rob's method worked for me. I updated the TTL index from weeks to 2 minutes and the previously loaded in documents started to be removed from the database once they exceeded the new expiration period.

EDIT: note that if authentication is enabled for Mongo then the 'collMod' command requires dbAdmin or adAminAnyDatabase privileges

Higgle answered 28/2, 2019 at 18:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.