0

1) We have put expireAfterSeconds=15 on column of type: date

[
    {
        "v" : 1,
        "key" : {
            "_ts" : -1
        },
        "name" : "AnjaliIndex",
        "ns" : "test.sessions",
        "expireAfterSeconds" : 15
    }
]

It is working fine on yesterdays date but is not working fine on todays date i.e it is removing data when i change document date from current date to yesterdays date where it should delete all data. (Current date which i given is even not future time but previous time)

Why is this happening? Is there any particular cycle or time when mongodb engine collect documents for expiry?

(I have seen related question but in that question use case is different where he was giving future date)

Mongo DB Version: 3.2.22

Sample Document:(not gettinkg deleted)

{
    "_id" : ObjectId("5dde452818c87122389bbc09"),
    "authorization" : "a0ce0b43-194d-4402-99cb-b660b3365757",
    "userNumber" : "gourav@gmail.com",
    "_ts" : ISODate("2019-11-27T13:43:04.776Z")
}
fatherazrael
  • 5,511
  • 16
  • 71
  • 155

1 Answers1

0

I will try to answer and see if that can help you.

db.my_collection.createIndex( { "createdAt": 1 }, { expireAfterSeconds: 3600 } )

After that, every document that you insert in this collection must have the "createdAt" with the current date:

db.myCollection.insert( {
   "createdAt": new Date(), // This can be set in UTC
   "dataExample": 2,
   "Message": "#### My data ####"
} )
Sohan
  • 6,252
  • 5
  • 35
  • 56