I'm designing my back-end. I have a json array/queue/something which I only need any data that is at most 2 weeks old, that is continuously appended to. I only want to delete from this "queue", but not the container document. Can I use TTL for this, or does TTL only work for whole documents?

Is there a better way to do this? Should I store them in per-day or per-hour arrays as separate documents instead?

Running couchbase 2.2.

有帮助吗?

解决方案

TTL in Couchbase only applies to whole documents, it's not possible to expire subsets of a document. Like you said you can always have separate documents with different expiry times in which you have a type,date and then the array of data as an element.

Then using a view like so:

function (doc, meta) {
  if(meta.type == "json") {
    if(doc.type == "ordered_data") {
      if(doc.date) {
        emit(dateToArray(doc.date)); 
      }
    }
  }
}

You could emit all the related data ordered by date (flag descending set to true), it'd also allow your app to select specific dates by passing in one or more keys. I.e. selecting a date range of 2days,1week etc. When the document expires it'd be removed from the view when it updates (varies based upon your stale parameters plus ops a second/time).

Then you can do whatever joining or extra processing you need at the application layer. There are other options available but for me this would be the most sensible way to approach the problem, any problems just comment and we'll try again.

P.s. How big are you arrays going to become? If they are going to be very large then perhaps you'd need to look at a different tech or way to solve the problem.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top