Mongodb TTL expires documents early

2024/9/28 0:38:48

I am trying insert a document into a Mongo database and have it automatically expire itself after a predetermine time. So far, my document get inserted but always get deleted from the database from 0 - 60 seconds even though I set the 'expireAfterSeconds' to much longer. I know mongodb deletes expired documents about every 60 seconds so it seems the 'expredAfterSeconds' variable is not working.

I followed the documentation here: Mongodb TTL Docs

Here is my test code that should expire (delete) the document after 3 minutes (but it does it under a minute):

import pymongo
import datetimemongo_con = pymongo.Connection('localhost', 27017)
mongo_db = mongo_con.Mongo_database
mongo_col = mongo_db.my_TTL_collectiontimestamp = datetime.datetime.now()mongo_col.ensure_index("date", expireAfterSeconds=3*60)                     mongo_col.insert({'_id': 'login_session', "date": timestamp, "session": "test session"})

Does anybody have any ideas what the problem is?

Answer

Your problems come from using naive timestamps in your local timezone. The FAQ of pymongo has an entry which includes a warning not to use datetime.datetime.now(). Using utcnow, the ttl-setting works as expected:

import pymongo
import datetimemongo_con = pymongo.Connection('localhost', 27017)
mongo_db = mongo_con.Mongo_database
mongo_col = mongo_db.my_TTL_collectiontimestamp = datetime.datetime.now()
utc_timestamp = datetime.datetime.utcnow()mongo_col.ensure_index("date", expireAfterSeconds=3*60)                     mongo_col.insert({'_id': 'session', "date": timestamp, "session": "test session"})
mongo_col.insert({'_id': 'utc_session', "date": utc_timestamp, "session": "test session"})
# the utc_session will be deleted after around 3 minutes, 
# the other depending on your timezone
https://en.xdnf.cn/q/71324.html

Related Q&A

sqlalchemy, hybrid property case statement

This is the query Im trying to produce through sqlalchemySELECT "order".id AS "id", "order".created_at AS "created_at", "order".updated_at AS "u…

Whats the newest way to develop gnome panel applets (using python)

Today Ive switched to GNOME (from XFCE) and found some of the cool stuff missing and I would like to (try to) do them on my own. I tried to find information on how to develop Gnome applets (items you p…

How to install opencv-python in python 3.8

Im having problem during the installation of opencv-python in pycharm. After opening pycharm I click on settings and then project interpreter, I click on + and search for the correct module, I started …

python augmented assignment for boolean operators

Does Python have augmented assignment statements corresponding to its boolean operators?For example I can write this:x = x + 1or this:x += 1Is there something I can write in place of this:x = x and yT…

Change a pandas DataFrame column value based on another column value

I have a dataframe with two columns each of which represents an organism. They are called ORG1 and ORG2 I want to move the values of ORG2 into ORG1 for the corresponding index value.So, if ORG1 is A a…

How to lock a sqlite3 database in Python?

Is there a way to explicitly acquire a lock on a sqlite3 database in Python?

How to install pyzmq on an Alpine Linux container?

I have a container with the python:3.6-alpine kernel. I have a problem installing the pyzmq via pip on this: Dockerfile: FROM python:3.6-alpineRUN mkdir /code RUN apk add vim WORKDIR / ADD . /codedocke…

How to Get Value Out from the Tkinter Slider (Scale)?

So, here is the code I have, and as I run it, the value of the slider bar appears above the slider, I wonder is there a way to get that value out? Maybe let a=that value. ;)from Tkinter import *contr…

Python cassandra-driver OperationTimeOut on every query in Celery task

I have a problem with every insert query (little query) which is executed in celery tasks asynchronously. In sync mode when i do insert all done great, but when it executed in apply_async() i get this:…

Function which returns the least-squares solution to a linear matrix equation

I have been trying to rewrite the code from Python to Swift but Im stuck on the function which should return the least-squares solution to a linear matrix equation. Does anyone know a library written i…