I want to know if it\'s possible to use TTL on nested documents.
I have Account
and inside I have Sessions
. Sessio
That is currently not possible with TTL index. Mongod will remove the whole document after a specified number of seconds or at a specific clock time.
TTL relies on a background thread in mongod that reads the date-typed values in the index and removes expired documents from the collection.
I would recommend that you store the session sub-document in a separate collection and add a TTL index on that collection.
If you can't change your schema, the alternative is to create a background job that will delete nested documents from your collection every 60 seconds.