[MongoDB]: TTL (Time To Live) collections and Indexes

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.
Data expiration is useful for certain types of information like machine generated event data, logs, and session information that only need to persist in a database for a finite amount of time.

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

Expire Data from Collections by Setting 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.
> use dbversity
switched to db dbversity
>
> show collections
>
>
> db.dbversity_log.createIndex( { “loggedAt” : 1 } , { expireAfterSeconds : 60 } )
{
“createdCollectionAutomatically” : true,
“numIndexesBefore” : 1,
“numIndexesAfter” : 2,
“ok” : 1
}
>
>
> show collections
dbversity_log
system.indexes
>
>
> db.dbversity_log.insert( { logid : 1, loggedAt : new Date(), logInfo : “Log is being generated … please wait for a moment !” } )
WriteResult({ “nInserted” : 1 })
> db.dbversity_log.insert( { logid : 2, loggedAt : new Date(), logInfo : “Log is being generated … please wait for a moment !” } )
WriteResult({ “nInserted” : 1 })
> db.dbversity_log.insert( { logid : 3, loggedAt : new Date(), logInfo : “Log is being generated … please wait for a moment !” } )
WriteResult({ “nInserted” : 1 })
> db.dbversity_log.insert( { logid : 4, loggedAt : new Date(), logInfo : “Log is being generated … please wait for a moment !” } )
WriteResult({ “nInserted” : 1 })
> db.dbversity_log.insert( { logid : 5, loggedAt : new Date(), logInfo : “Log is being generated … please wait for a moment !” } )
WriteResult({ “nInserted” : 1 })
>
> db.dbversity_log.find()
{ “_id” : ObjectId(“552796c69689e4b8db19b607”), “logid” : 1, “loggedAt” : ISODate(“2015-04-10T09:24:22.632Z”), “logInfo” : “Log is being generated … please wait for a moment !” }
{ “_id” : ObjectId(“552796c69689e4b8db19b608”), “logid” : 2, “loggedAt” : ISODate(“2015-04-10T09:24:22.635Z”), “logInfo” : “Log is being generated … please wait for a moment !” }
{ “_id” : ObjectId(“552796c69689e4b8db19b609”), “logid” : 3, “loggedAt” : ISODate(“2015-04-10T09:24:22.637Z”), “logInfo” : “Log is being generated … please wait for a moment !” }
{ “_id” : ObjectId(“552796c69689e4b8db19b60a”), “logid” : 4, “loggedAt” : ISODate(“2015-04-10T09:24:22.638Z”), “logInfo” : “Log is being generated … please wait for a moment !” }
{ “_id” : ObjectId(“552796c89689e4b8db19b60b”), “logid” : 5, “loggedAt” : ISODate(“2015-04-10T09:24:24.539Z”), “logInfo” : “Log is being generated … please wait for a moment !” }
>
> db.dbversity_log.find().pretty()
{
“_id” : ObjectId(“552796c69689e4b8db19b607”),
“logid” : 1,
“loggedAt” : ISODate(“2015-04-10T09:24:22.632Z”),
“logInfo” : “Log is being generated … please wait for a moment !”
}
{
“_id” : ObjectId(“552796c69689e4b8db19b608”),
“logid” : 2,
“loggedAt” : ISODate(“2015-04-10T09:24:22.635Z”),
“logInfo” : “Log is being generated … please wait for a moment !”
}
{
“_id” : ObjectId(“552796c69689e4b8db19b609”),
“logid” : 3,
“loggedAt” : ISODate(“2015-04-10T09:24:22.637Z”),
“logInfo” : “Log is being generated … please wait for a moment !”
}
{
“_id” : ObjectId(“552796c69689e4b8db19b60a”),
“logid” : 4,
“loggedAt” : ISODate(“2015-04-10T09:24:22.638Z”),
“logInfo” : “Log is being generated … please wait for a moment !”
}
{
“_id” : ObjectId(“552796c89689e4b8db19b60b”),
“logid” : 5,
“loggedAt” : ISODate(“2015-04-10T09:24:24.539Z”),
“logInfo” : “Log is being generated … please wait for a moment !”
}
>
>
> db.dbversity_log.insert( { logid : 6, loggedAt : new Date(), logInfo : “Log is being generated … please wait for a moment !” } )
WriteResult({ “nInserted” : 1 })
>
> db.dbversity_log.find()
{ “_id” : ObjectId(“552796c69689e4b8db19b607”), “logid” : 1, “loggedAt” : ISODate(“2015-04-10T09:24:22.632Z”), “logInfo” : “Log is being generated … please wait for a moment !” }
{ “_id” : ObjectId(“552796c69689e4b8db19b608”), “logid” : 2, “loggedAt” : ISODate(“2015-04-10T09:24:22.635Z”), “logInfo” : “Log is being generated … please wait for a moment !” }
{ “_id” : ObjectId(“552796c69689e4b8db19b609”), “logid” : 3, “loggedAt” : ISODate(“2015-04-10T09:24:22.637Z”), “logInfo” : “Log is being generated … please wait for a moment !” }
{ “_id” : ObjectId(“552796c69689e4b8db19b60a”), “logid” : 4, “loggedAt” : ISODate(“2015-04-10T09:24:22.638Z”), “logInfo” : “Log is being generated … please wait for a moment !” }
{ “_id” : ObjectId(“552796c89689e4b8db19b60b”), “logid” : 5, “loggedAt” : ISODate(“2015-04-10T09:24:24.539Z”), “logInfo” : “Log is being generated … please wait for a moment !” }
{ “_id” : ObjectId(“552797119689e4b8db19b60c”), “logid” : 6, “loggedAt” : ISODate(“2015-04-10T09:25:37.404Z”), “logInfo” : “Log is being generated … please wait for a moment !” }
>
> new Date()
ISODate(“2015-04-10T09:26:08.592Z”)
>
>
> db.dbversity_log.find()
{ “_id” : ObjectId(“552797119689e4b8db19b60c”), “logid” : 6, “loggedAt” : ISODate(“2015-04-10T09:25:37.404Z”), “logInfo” : “Log is being generated … please wait for a moment !” }
>
>
> new Date()
ISODate(“2015-04-10T09:26:18.890Z”)
>
> db.dbversity_log.find()
{ “_id” : ObjectId(“552797119689e4b8db19b60c”), “logid” : 6, “loggedAt” : ISODate(“2015-04-10T09:25:37.404Z”), “logInfo” : “Log is being generated … please wait for a moment !” }
>
>
>
> new Date()
ISODate(“2015-04-10T09:26:38.120Z”)
>
>
> db.dbversity_log.find()
>
>

  • Ask Question