So I have a mongo collection called 'abcd', inside my collection I am creating indexes called 'token'. I understand that TTL needs to be tied to a ISO datetime object in mongo as an index, or as part of an array.
I was unsuccessful at getting the token to expire when adding an index to my 'token'. So I am adding a created_timestamp
ISO date object into my "data" array of my token index.
My question is, how can I use my expireAfterSeconds=10
attribute to the object of created_timestamp of my data array. My goal is to get the entire token index to delete after 10 seconds. Here is a json of what my 'token' document looks like:
{
"_id" : ObjectId("5a7dec4189284441fe9aa1fc"),
"token" : "RfAQ3W",
"data" : "{\"token\": {\"user_id\": 308, \"exp\": 151821111, \"team_code\": 1022, \"created_timestamp\": \"2018-02-09T18:45:24.823785\}}"
}
I want to be able to expire my token after 10 seconds from the created_timestamp
which is the ISO date of when the token gets generated to mongodb
I am using: Pymongo 3.4 Mongoengine 0.13 Python 2.7
Why don't you create a new field 'created_at' with Date() value and make it TTL index instead of putting this in an array?
After all this whole document is going to delete after 10 sec.