4

I'm trying to get Mongo to remove documents with the TTL feature however without success. Have tried many things but mongo doesn't seem to clean up.

My index:

    {
    "v" : 1,
    "key" : {
        "date" : 1
    },
    "name" : "date_1",
    "ns" : "history.history",
    "expireAfterSeconds" : 60
}

The date value from document:

       "date" : "2016-09-29 11:08:46.461207",

Output from db.serverStatus().metrics.ttl:

{ "deletedDocuments" : NumberLong(0), "passes" : NumberLong(29) }

Time output from db.serverStatus():

"localTime" : ISODate("2016-09-29T11:19:45.345Z")

Only thing I suspect is the way I insert the value from Python. Could be that it's in some way wrong. I have a JSON document which contains the following element:

"date": str(datetime.utcnow()), 

Any clues where the problem might lay?

Thanks, Janis

1 Answers1

3

As you have guessed, the problem is in how you insert the date value. I'll quote the docs:

If the indexed field in a document is not a date or an array that holds a date value(s), the document will not expire.

You are casting the date to a string. If you are using the pymongo driver, he will handle datetimes nicely and convert it to MongoDB native Date type.

This way, the following should work:

"date": datetime.utcnow()
joao
  • 4,067
  • 3
  • 33
  • 37
  • Thanks for the tip. Tried that and get exception: datetime.datetime(2016, 9, 30, 6, 33, 2, 771754) is not JSON serializable. I guess due to the fact that I'm executing json.dumps() on the prepered JSON document. – Janis Jansons Sep 30 '16 at 06:35
  • @JanisJansons pymongo handles python dictionaries and lists and most types correctly, no need to use json.dumps(). – joao Sep 30 '16 at 10:36
  • thanks. How silly of me doing that ;) Removed the code and all is good now, can see the cleanup working as expected. Thanks all. – Janis Jansons Sep 30 '16 at 11:17