1

My understanding is that Couchbase views are built incrementally, but I can't seem to find an answer to whether a document can exist in a view multiple times. For example, say I want to create a view based on an updatedAt timestamp, that is changed every time I update this document type.

If the view is built incrementally, that seems to imply that if document id "1234" is updated several times and that updatedAt timestamp changed each time, I'd end up with several entries in the view for the same document, when what I want is just one entry, for the latest value.

It does seem like Couchbase is limiting it to a single copy of any given document id within the view, but I can't find firm confirmation of that anywhere. I want to make sure I'm not designing something for a production system around a behavior that might not work the way it seems to on a small scale.

  • If you're designing a new system, one thing to consider is that views are old tech that's likely to be deprecated in a future version of Couchbase Server. The modern alternative is to use N1QL queries. If you store the `updatedAt` value in ISO-8601 format, you can create an index on this field and use a date range query to efficiently select documents updated in a certain time period. – dnault Jun 04 '21 at 21:54
  • I should say the timestamp could also be a numeric value (epoch seconds, etc.) and it would still be indexable. – dnault Jun 04 '21 at 22:05
  • That's actually really good information - we'd been looking at views as a lower-impact solution, since it seems like having everything pre-calculated would lead to better performance. We use indexes for some scenarios, but it sounds like we should plan on moving away from views in general. Thanks for that info! – user1778117 Jun 06 '21 at 13:50

1 Answers1

1

Yes. When a view index is refreshed, any documents modified since the last refresh have their associated rows removed from the view, and the map function is invoked again to emit the new row(s).

A single document can generate multiple view rows, but only if the view's map function calls emit multiple times.

dnault
  • 8,340
  • 1
  • 34
  • 53