Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongodb different TTL for every document

Tags:

mongodb

ttl

as far i know, currently mongodb TTL are managed by expireAfterSeconds index and it's setting for all document inside a collection.

so is there any built-in way to set expiration to single document ?, thankyou

like image 572
Lobo Nokewe Ngewe Avatar asked Apr 25 '20 10:04

Lobo Nokewe Ngewe


People also ask

How does TTL work in MongoDB?

TTL (Time-To-Live) indexes are special single-field indexes that MongoDB can use to automatically remove documents from a collection after a certain amount of time. A background thread in MongoDB reads the values in the index and removes expired documents from the collection (usually every minute).

Does MongoDB have TTL?

TTL collections make it possible to store data in MongoDB and have the mongod automatically remove data after a specified number of seconds or at a specific clock time.

What is the method used for creating TTL index with the expireAfterSeconds option on a field whose value is either a date or an array that contains date values?

To create a TTL index, use the createIndex() method on a field whose value is either a date or an array that contains date values, and specify the expireAfterSeconds option with the desired TTL value in seconds.

How many documents can MongoDB handle?

Mongo can easily handle billions of documents and can have billions of documents in the one collection but remember that the maximum document size is 16mb. There are many folk with billions of documents in MongoDB and there's lots of discussions about it on the MongoDB Google User Group.


1 Answers

On each documet you can set a field of expires and create the following index:

db.docs.createIndex( { expires:1 }, {expireAfterSeconds: 0 } );

documents inside this collection will get removed once time ticks over the expired field. However, this may take up 60 seconds as the background task that runs to remove documents runs every 60 seconds.

Here's an example though...

So let's insert a bunch of documents adding 60 seconds to each document each time we add them.

> var expires = new Date(); // Gets the current datetime.
> expires.setSeconds(expires.getSeconds() + 60)
1587812623023
> db.docs.insert({expires})
WriteResult({ "nInserted" : 1 })

> expires.setSeconds(expires.getSeconds() + 60)
1587812683023
> db.docs.insert({expires})
WriteResult({ "nInserted" : 1 })

> expires.setSeconds(expires.getSeconds() + 60)
1587812743023
> db.docs.insert({expires})
WriteResult({ "nInserted" : 1 })

> db.docs.find()
{ "_id" : ObjectId("5ea418de00f07c4d6461090b"), "expires" : ISODate("2020-04-25T11:03:43.023Z") }
{ "_id" : ObjectId("5ea418e300f07c4d6461090c"), "expires" : ISODate("2020-04-25T11:04:43.023Z") }
{ "_id" : ObjectId("5ea418e600f07c4d6461090d"), "expires" : ISODate("2020-04-25T11:05:43.023Z") }

Now if we add the TTL index.

db.docs.createIndex( { expires:1 }, { expireAfterSeconds: 0 } )

Then we can monitor our collection each 60 seconds and see each document getting removed.

> new Date()
ISODate("2020-04-25T11:03:28.278Z")
> db.docs.find()
{ "_id" : ObjectId("5ea418de00f07c4d6461090b"), "expires" : ISODate("2020-04-25T11:03:43.023Z") }
{ "_id" : ObjectId("5ea418e300f07c4d6461090c"), "expires" : ISODate("2020-04-25T11:04:43.023Z") }
{ "_id" : ObjectId("5ea418e600f07c4d6461090d"), "expires" : ISODate("2020-04-25T11:05:43.023Z") }

Nothing has been removed yet.

> new Date()
ISODate("2020-04-25T11:04:18.652Z")
> db.docs.find()
{ "_id" : ObjectId("5ea418e300f07c4d6461090c"), "expires" : ISODate("2020-04-25T11:04:43.023Z") }
{ "_id" : ObjectId("5ea418e600f07c4d6461090d"), "expires" : ISODate("2020-04-25T11:05:43.023Z") }

One document has gone.

> new Date()
ISODate("2020-04-25T11:05:17.705Z")
> db.docs.find()
{ "_id" : ObjectId("5ea418e600f07c4d6461090d"), "expires" : ISODate("2020-04-25T11:05:43.023Z") }

Another is gone.

> new Date()
ISODate("2020-04-25T11:06:31.390Z")
> db.docs.find()
>

and we're left with no doucments in our collection.

like image 159
Kevin Smith Avatar answered Oct 22 '22 12:10

Kevin Smith