Arnab Chakraborty
Arnab Chakraborty

Reputation: 7472

Returning full document from result of a view query for Couchbase

I'm trying to build a filter view, where I can filter documents based on a column range. My map function looks something like this:

function (doc, meta) {
  if(!doc.tombstone) {
    emit([doc.effectiveDateTime]);
  }
}

When I query this view from code, the result only has the keys and not the documents.

I can think of the following ways to solve this, and need help deciding which one could work.

  1. setIncludeDocs: Exploring the documentation a little bit, I came accross this java code snippet where the setIncludeDocs method is invoked on the query to return full documents with the result. It looks promising, unfortunately I cannot find an equivalent of this method in the C# SDK.

  2. Query individual documents: Emit the document id as the value like emit([doc.effectiveDateTime], meta.id); and query each document separately from the client side. I do not want to use this approach as the client would end up making multiple calls to the server to get the full result set and would cause performance issues in the client side.

  3. Emit entire document: I could emit the entire document as the value in the emit statement of the mapper like this: emit([doc.effectiveDateTime], doc);. This approach has the advantage that the client has to make only one call to get the result set. But I'm not sure about the implications this has on the size of the index. Given that my mapper will work on most of the documents, would the index size grow or will couchbase be able to optimize it internally somehow?

  4. Query documents inside the reduce function (not sure if this is feasible): If I modify the mapper to emit document ids as values and have a mechanism to query and get the entire document from inside the reducer, then I should be able form the result set inside the reducer. Are there any javascript apis which can be used to acces the database from inside the reducer?

Any advice regarding the above points or any new ones would be much appreciated.

Upvotes: 2

Views: 650

Answers (1)

Simon Baslé
Simon Baslé

Reputation: 28301

In the C# SDK 2.x, your best bet is option 2. with a twist: the query engine already emits the document's ID as part of its response metadata, no need for your view to emit it.

Use the Rows method of your IViewResult to iterate over rows. This gives you a ViewRow<T> of each row. On ViewRow, just get the Idproperty. You can then get the associated document the usual way.

Upvotes: 1

Related Questions