How to achieve sub-document (JSON) expiry in Couchbase?

Hi All,
I have a requirement as described below. For example, the application creates the JSON document shown below, which contains multiple sub-documents, each with a defined expiry/TTL. I want to delete a sub-document if it has expired (expiry < current time). How can I achieve this? If I use the DCP client, there will be significant data transfer between the application and the database. I am looking for an efficient solution.

A document:
{
“sub-doc-1”:
{
“data”: “”,
“expiry”: “time-1”
},
“sub-doc-2”:
{
“data”: “”,
“expiry”: “time-2”
}
}

Hi @creddym - couchbase document expiry is on whole documents. It looks like you’ve implemented your own expiry on subdocuments. You could find documents with expired sub-docs by periodically running a n1ql query, and then for every result, issue a mutateIn to delete the expired sub-docs.

@creddym yes as Michael says, it’s not directly supported (I don’t know of any document database that will automatically remove specific chunks of a document on a TTL in fact).
Personally I would look at refactoring the model so that each sub-object you want to have a TTL becomes a standalone document.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.