Search code examples
mongodbttlmongodb-indexes

mongodb background TTL not removing documents


I am using TTL indexes on Mongo 2.4.10 but it doesn't work anymore. For instance, on one collection, I've set it to 5 days (432000 seconds) as we can see with db.collectionName.getIndexes() :

    {
            "v" : 1,
            "key" : {
                    "date" : -1,
                    "background" : true,
                    "expireAfterSeconds" : 432000
            },
            "ns" : "dbName.collectionName",
            "name" : "date_-1_background__expireAfterSeconds_432000"
    }

But a single findOne() shows me an older document than expected :

    "_id" : ObjectId("53a058140cf25876d78f7d03"),
    "_class" : 
    "productIds" : [
            NumberLong(1045),
            NumberLong(1124),
            NumberLong(1277),
            NumberLong(800),
            NumberLong(978)
    ],
    "userId" : NumberLong(214120),
    "date" : ISODate("2014-06-16T11:45:21.341Z")

The java code to create the TTL index is the following :

    DBObject keys = new BasicDBObject();
    keys.put(fieldName, -1);
    keys.put("background", true);
    keys.put("expireAfterSeconds", ttlInSeconds);

    database.getCollection(collectionName).ensureIndex(keys);

All seemed to work fine until recently : we didn't notice it before in production. This is happening on all my databases and all concerned collections.

What's wrong ?


EDIT :

I've checked my server configuration, TTL monitor is enabled:

my_replicat:PRIMARY> db.adminCommand({getParameter:1, ttlMonitorEnabled:1 })
{ "ttlMonitorEnabled" : true, "ok" : 1 }

Solution

  • You're adding background and expireAfterSeconds as additional fields of the index instead of as index options. Put those in a secondDBObject parameter to ensureIndex:

    DBObject keys = new BasicDBObject();
    keys.put(fieldName, -1);
    
    DBObject options = new BasicDBObject();
    options.put("background", true);
    options.put("expireAfterSeconds", ttlInSeconds);
    
    database.getCollection(collectionName).ensureIndex(keys, options);
    

    Note that you'll need to manually drop the existing index first before you can re-add it.