Question

I'm trying to get MongoDB to automatically delete documents using TTL, but I can't seem to get it to work. Here is an example document:

{
    "_id" : ObjectId("52f50824169cb8055393c01e"),
    "_cls" : "ExpiringPageView",
    "viewable" : ObjectId("523f3586169cb81d568e442f"),
    "ip" : "127.0.0.1",
    "datetime" : ISODate("2014-02-07T09:21:56.628Z")
}

And here are the indexes of the collection:

[
    {
        "v" : 1,
        "key" : {
            "_id" : 1
        },
        "ns" : "mysite.expiring_page_view",
        "name" : "_id_"
    },
    {
        "v" : 1,
        "key" : {
            "_cls" : 1,
            "datetime" : 1
        },
        "ns" : "mysite.expiring_page_view",
        "name" : "_cls_1_datetime_1",
        "background" : false,
        "expireAfterSeconds" : 60,
        "dropDups" : false
    }
]

I'm wondering if Mongo won't expire these documents due to the difference in time zone. When I enter new ISODate() in to the Mongo console I get a date/time that is 7 hours ahead (probably GMT) of the time in the document's datetime field. Could this be why my documents aren't expiring?

In case it helps, I'm using mongoengine as my document-object mapper.

Was it helpful?

Solution

Your TTL index is a compound index, because it consists of both _cls and datetime, but that is not supported. Make sure the TTL index is a simple, non-compound index on datetime alone.

OTHER TIPS

MongoDB servers (as many others) use UTC (Coordinated Universal Time), you can get the correct time to set by coding:

from datetime import datetime

now_utc = datetime.utcnow()
# ...
collection.insert({'datetime': now_utc, ...})
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top