0

I saw a similar question here but there the OP claims he has a typo.

mongodb TTL not removing documents

I don't think I have a typo. I created a collection by inserting 3 documents.
Then I added a TTL index on LastUpdated with expireAfterSecs = 120 seconds.
More then 3-4-5 mins later no documents are deleted.

Why is that? What am I doing wrong?

[test] 2014-03-26 18:35:35.154 >>> db.test_ttl.getIndexes();
[
        {
                "v" : 1,
                "key" : {
                        "_id" : 1
                },
                "ns" : "test.test_ttl",
                "name" : "_id_"
        },
        {
                "v" : 1,
                "key" : {
                        "LastUpdated" : 1
                },
                "ns" : "test.test_ttl",
                "name" : "LastUpdated_1",
                "expireAfterSecs" : 120
        }
]
[test] 2014-03-26 18:35:45.378 >>> db.test_ttl.find();
{
        "_id" : ObjectId("5333556a426e7309c6213d19"),
        "x" : 1,
        "LastUpdated" : ISODate("2014-03-26T22:32:10.628Z")
}
{
        "_id" : ObjectId("5333556e426e7309c6213d1a"),
        "x" : 2,
        "LastUpdated" : ISODate("2014-03-26T22:32:14.976Z")
}
{
        "_id" : ObjectId("53335581426e7309c6213d1b"),
        "x" : 100,
        "LastUpdated" : ISODate("2014-03-26T22:32:33.912Z")
}
[test] 2014-03-26 18:35:50.58 >>> new Date();
ISODate("2014-03-26T22:35:52.819Z")
[test] 2014-03-26 18:35:52.835 >>> new Date();
ISODate("2014-03-26T22:36:03.927Z")
[test] 2014-03-26 18:36:03.927 >>> db.test_ttl.find();
{
        "_id" : ObjectId("5333556a426e7309c6213d19"),
        "x" : 1,
        "LastUpdated" : ISODate("2014-03-26T22:32:10.628Z")
}
{
        "_id" : ObjectId("5333556e426e7309c6213d1a"),
        "x" : 2,
        "LastUpdated" : ISODate("2014-03-26T22:32:14.976Z")
}
{
        "_id" : ObjectId("53335581426e7309c6213d1b"),
        "x" : 100,
        "LastUpdated" : ISODate("2014-03-26T22:32:33.912Z")
}
[test] 2014-03-26 18:36:06.376 >>> new Date();
ISODate("2014-03-26T22:36:07.671Z")
[test] 2014-03-26 18:36:07.671 >>>
[test] 2014-03-26 18:39:32.733 >>> db.test_ttl.find();
{
        "_id" : ObjectId("5333556a426e7309c6213d19"),
        "x" : 1,
        "LastUpdated" : ISODate("2014-03-26T22:32:10.628Z")
}
{
        "_id" : ObjectId("5333556e426e7309c6213d1a"),
        "x" : 2,
        "LastUpdated" : ISODate("2014-03-26T22:32:14.976Z")
}
{
        "_id" : ObjectId("53335581426e7309c6213d1b"),
        "x" : 100,
        "LastUpdated" : ISODate("2014-03-26T22:32:33.912Z")
}
[test] 2014-03-26 18:39:44.355 >>> new Date();
ISODate("2014-03-26T22:39:46.086Z")
[test] 2014-03-26 18:39:46.102 >>>
Community
  • 1
  • 1
peter.petrov
  • 38,363
  • 16
  • 94
  • 159

1 Answers1

1

OK, never mind. This is also a typo. It's
a typo in the book I am reading (page 114).

http://www.oreilly.com/catalog/errata.csp?isbn=0636920028031

The correct option is expireAfterSeconds not expireAfterSecs.
So I didn't create the TTL index properly due to this typo.
Realized this 4-5 minutes after asking the question.

peter.petrov
  • 38,363
  • 16
  • 94
  • 159