[SERVER-12482] TTL using microseconds won't work anymore Created: 25/Jan/14  Updated: 10/Dec/14  Resolved: 29/Jan/14

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: 2.4.9
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Alessio Periloso Assignee: Bernie Hackett
Resolution: Cannot Reproduce Votes: 0
Labels: expireAfterSeconds, ttl
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Debian 6.0.8


Operating System: Linux
Steps To Reproduce:

mongo:
db.app.events.ensureIndex(

{ "expireAt": 1 }

,

{ expireAfterSeconds: 0 }

)

using pymongo:
expires = (datetime.datetime.now() + datetime.timedelta(0,60))

toBeCached =

{'url': link, 'code': code, 'headers': headers, 'content': content, 'expireAt': expires}

pages.insert(toBeCached)

Participants:

 Description   

Setting the content to expire at a certain date with microseconds causes the expiration not to happen.
I've used the mongod with python and the date, by default, was outputted with microseconds, so I did notice the issue.

Cheers from Italy!



 Comments   
Comment by Bernie Hackett [ 29/Jan/14 ]

Glad it's working for you. Let us know if you run into any issues.

Comment by Alessio Periloso [ 29/Jan/14 ]

For some circumstances, when I did try to set the time to utc before it didn't work; however I've tried your hint and it does work now.
Thank you very much for your support!

Kind regards,
Alessio Periloso

Comment by Bernie Hackett [ 28/Jan/14 ]

bigalex, I can't reproduce this. Following the example here:

http://docs.mongodb.org/manual/tutorial/expire-data/#expire-documents-at-a-certain-clock-time

PyMongo:

>>> import pymongo
>>> c = pymongo.MongoClient()
>>> db = c.app
>>> import datetime
>>> dt = datetime.datetime.utcnow() + datetime.timedelta(seconds=60)
>>> dt
datetime.datetime(2014, 1, 28, 22, 43, 29, 275127)
>>> db.events.insert({'expireAt': dt})
ObjectId('52e8326dfa5bd8366cde621d')
>>> db.events.find_one()
{u'_id': ObjectId('52e8326dfa5bd8366cde621d'), u'expireAt': datetime.datetime(2014, 1, 28, 22, 43, 29, 275000)}
>>> datetime.datetime.utcnow()
datetime.datetime(2014, 1, 28, 22, 43, 10, 489107)
>>> db.events.find_one()
{u'_id': ObjectId('52e8326dfa5bd8366cde621d'), u'expireAt': datetime.datetime(2014, 1, 28, 22, 43, 29, 275000)}
>>> # The document has been deleted by the TTL Monitor
>>> ...
>>> db.events.find_one()
>>> datetime.datetime.utcnow()
datetime.datetime(2014, 1, 28, 22, 44, 36, 573480)

The problem might be that you used datetime.datetime.now(). MongoDB expects UTC aware timestamps. See this PyMongo FAQ entry:

http://api.mongodb.org/python/current/faq.html#what-is-the-correct-way-to-handle-time-zones-with-pymongo

Generated at Thu Feb 08 03:28:39 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.