Search code examples
mongodbmongo-shell

Documents not expiring using TTL in mongodb 2.6.7


I'm trying the TTL feature in a mongo shell but I cannot seem to make it work. I have triple check everything using the documentation.

Here is what I did:

MongoDB shell version: 2.6.7
connecting to: test

> db.streamers.getIndexes()
[
        {
                "v" : 1,
                "key" : {
                        "_id" : 1
                },
                "name" : "_id_",
                "ns" : "test.streamers"
        },
        {
                "v" : 1,
                "key" : {
                        "room" : 1
                },
                "name" : "room_1",
                "ns" : "test.streamers",
                "background" : true,
                "safe" : null
        },
        {
                "v" : 1,
                "key" : {
                        "lastAlive" : 1
                },
                "name" : "lastAlive_1",
                "ns" : "test.streamers",
                "expiresAfterSeconds" : 60,
                "background" : true,
                "safe" : null
        }
]

> db.streamers.insert({ _id: "hello", room: "yop", lastAlive: new Date() })
WriteResult({ "nInserted" : 1 })

[waiting for a while here...]

> db.streamers.find({ _id: "hello" })
{ "_id" : "hello", "room" : "yop", "lastAlive" : ISODate("2015-02-18T13:03:02.836Z") }

> new Date()
ISODate("2015-02-18T13:50:50.403Z")

So clearly, the document is not being removed even after waiting for more than an hour. db.currentOp() returns an empty array as well.

This is a dev environment so mongodb is in a standalone configuration with default settings. A getParameter on ttlMonitorEnabled returns true so that's fine too.

What is wrong here ?


Solution

  • There was a typo in your index creation.

    The setting expiresAfterSeconds should be expireAfterSeconds.