How does the TTL expiry work?

I have a program written in Go. The program writes into mongo a certain document, this type of document have a TTL set from user input (between 1-7 days).

Now the catch is here, during my testing I set the document to expire after 35 minutes. I updated my code to make sure it adheres to the time set from user input. However, when I checked the saved document, it was deleted after 35 minutes although I set it (as user input) to be valid for about 1-day.
Is there something I am missing? Does it mean when a TTL index is created it automatically sets the value of the expiration date to be the value from the first document that was saved? here is a snippet code:

type object struct {
ExpireAt time.Time

obj := object{
ExpireAt: time.Now().Add(duration * 24 * time.Hour),

Note the duration variable is the users input. The index gets created successfully but once it is saved into the DB, the index value stays contant for any other document save.

The aim is that each document should hold its own TTL as specified by the user input.

Can anyone provide better insights on this? Is there something that I am missing?

If the timestamp is to be the expireAt value then set the expireAfterSeconds to 0 on the TTL index.

1 Like

I have that set already, and this is the only method I’ve seen for the #Go driver:
data := mongo.IndexModel{ Keys: bson.D{{Key: "ExpireAt", Value: 1}}, Options: options.Index().SetExpireAfterSeconds(0), }