TTL mongo for specific entry array

karma Source

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},

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



answered 6 months ago Kevin Smith #1

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?

comments powered by Disqus