0

I am attempting to expire a collection in Mongo using Casbah's ensureIndex API.

Based on this document http://docs.mongodb.org/manual/tutorial/expire-data/

I am using casbah's proposed ensureIndex API

 collection.ensureIndex(DBObject("status" -> 1, "expireAfterSeconds" -> 120))

to expire the collection in 2 minutes ...

The collection is not being evicted or expired.

Am I missing anything else here?

Thanks

conikeec
  • 209
  • 2
  • 14

2 Answers2

3

There are a couple things to check:

  1. Were you just following the docs to a T and tried to create an index on a status field which doesn't actually exist in your documents? (had to atleast ask...)
  2. Does the status field contain JUST dates? It can theoretically be mixed, but only documents with a date type would be considered for expiration.
  3. Have you checked your collection indexes to make sure the index was properly created?

To check for the index from the console run: db.collection.getIndexes(). If the index was created successfully, then double check you have the corresponding status fields in your documents and that they are proper dates.

Adding the index alone, doesn't create the date field for you - you would need to add it to the documents or use an existing date field that is not part of any other index.

Also note, from the docs:

TTL indexes expire data by removing documents in a background task that runs every 60 seconds

So, if you have a 120 second expiration, bear in mind, that its possible the documents could remain for 120 seconds up to 179 seconds, give or take, depending on when the document expired and the background task last ran.

edit: As noted in the comments - a collection itself cannot be removed based on a TTL index, the index only expires the documents in the collection.

kmfk
  • 3,821
  • 2
  • 22
  • 32
  • status is a date field and I managed to get it working ... It now expires all documents in the collection. Is there a method or flag to remove the collection as well, especially if all documents are evicted. – conikeec Aug 16 '13 at 07:12
  • Thanks for the detailed answer. Yes, the mongod instance runs every 'x' minutes, so there could be a potential skew in the TTL which is fine by me – conikeec Aug 16 '13 at 07:26
  • No, the collection itself can not be removed with TTL, just the documents in it. – Derick Aug 16 '13 at 08:54
  • Ah - yeah, when you said the `collection` wasn't being expired, I assumed you meant the documents. @Derick above is correct here. – kmfk Aug 16 '13 at 14:22
  • @kmfk, so add that to your answer ;-) – Derick Aug 16 '13 at 15:51
  • @conikeec, Maybe we should say others what the problem was with your collection? Presumably, it was incorrect syntax for the index creation? – Eddie Jamsession Dec 02 '13 at 15:09
0

I think, you are passing the Options in the wrong way.

It should be-

collection.ensureIndex(DBObject("status" -> 1), DBObject("expireAfterSeconds" -> 120))

Instead of-

collection.ensureIndex(DBObject("status" -> 1, "expireAfterSeconds" -> 120))
Kunal_Hire
  • 11
  • 1