MongoDb TTL on nested document is possible?

后端 未结 1 1038
挽巷
挽巷 2021-01-17 10:21

I want to know if it\'s possible to use TTL on nested documents.

Scenario

I have Account and inside I have Sessions. Sessio

1条回答
  •  伪装坚强ぢ
    2021-01-17 10:35

    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.

    0 讨论(0)
提交回复
热议问题