Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TTL mongo for specific entry array

I read that mongodb has TTL (Time to live) indexes that can be activated for document.

But does it work if document structure is as follows?

username: 'user x',
activity: [
  {type:a, desc:1, timestamp:timestamp},
  {type:b, desc:2, timestamp:timestamp},
  {type:b, desc:3, timestamp:timestamp},
  etc.
]

Is there possibility to set TTL based on timestamp+7days of each array item so that only those expires but recent ones are kept?

like image 444
karma Avatar asked Oct 20 '25 17:10

karma


1 Answers

Read the documentation carefully, The TTL index can be applied to an array but it will delete the whole document when expired not just the element inside the array.

However, you could split the array out into many documents?

like image 187
Kevin Smith Avatar answered Oct 22 '25 08:10

Kevin Smith



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!