MongoDB TTL Indexes

1,070 views
Skip to first unread message

Sudheer Junk

unread,
Apr 20, 2017, 7:58:55 PM4/20/17
to mongodb-user
I am new to Mongo. This might be a basic question, but please help me.

How do I know if my index is a TTL index. when i query the indexes on a collection using getIndexes i get only two values

how do i know what value is set for expireAfterSeconds 

db.Col_T.getIndexes()

[

{

"v" : 1,

"key" : {

"CORPID" : 1,

"REGION" : 1

},

"name" : "CORPID_1_REGION_1",

"ns" : "b-stg.Col_T"

}

]

Akshat

unread,
Apr 26, 2017, 12:19:16 AM4/26/17
to mongodb-user
Hi Sudheer,

Hi 

How do I know if my index is a TTL index.
 
TTL Indexes indexes are single-field indexes that MongoDB can use to remove documents from a collection after a certain amount of time or at a specific clock time. You can specify the time expiration as expireAfterSeconds parameter. 

Below is an example how you can create  TTL Index:
 db.Col_T.createIndex( {"startTimeLocal":1},{ "expireAfterSeconds":360000})


TTL indexes can be identified from the output of  getIndexes().A TTL index entry would include a field "expireAfterSeconds" which specifies the expiry of the document in the collection.

> db.Col_T.getIndexes();


 
{
 
"v" : 2,
 
"key" : {
 
"startTimeLocal" : 1
 
},
 
"name" : "startTimeLocal_1",
 
"ns" : "local.startup_log",
 
"expireAfterSeconds" : 360000
 
}


db.Col_T.getIndexes()
[
{
"v" : 1,
"key" :{ "CORPID" : 1, "REGION" : 1 },
"name" : "CORPID_1_REGION_1",
"ns" : "b-stg.Col_T"
}
]


Based on the output of getIndexes() that you've provided, CORPID_1_REGION_1 is a compound index, and compound indexes do not support TTL.




Thanks,
Akshat
Reply all
Reply to author
Forward
0 new messages