Bucket.get to fetch only desired elements

Hi ,

I am using bucket.get(id) to fetch all the elements of the documents. In my case the document has many elements(rows) and I do not always use all the elements of the documents. I wanted to know is there a way to use bucket.get to fetch only desired elements(rows) ?

I understand that I can use Select query to fetch only desired elements of the document.

N1QL is one option with a SELECT query. However, if you only want that for a set of documents for which you know the IDs (or a single document), then maybe one of Couchbase 4.5 new features could be interesting to you: subdocument.

With subdocument, you can do a “partial get”: provide the key to a document + the JSON path to retrieve inside of it and it will only transfer that subset of the document. Path can even be something like “foo.bar.subArray[3]” to get a particular array element for instance.

Thank You @simonbasle .
Using subdocument, I can fetch only desired fields of document and thus improving the performance where only this subdocument is passed over network (Couchbase server to java application).

I also wanted to understand, in terms of performance (time taken to get document on server + transfer the complete document over network) which is beneficial,

  1. “Using bucket.get to fetch the document and once I have java object of the document then use only desired fields” OR
  2. Fetch only desired fields using sub-document and transfer it over network.

It depends on the size of your document really… Subdocument was built to address the use case where you have large documents and only need to work with a tiny subset of values inside of them. Not only read, but also mutate. Having to transfer all of the document in these cases would mean larger bandwidth usage and also higher latency.

I don’t have a general rule of thumb for choosing between kv get and subdoc, that would depend on your expected document size, expected subset to work with and also the performance of your network. But it should be pretty easy once you have an average size in mind to create such a document and measure both kv get and subdoc get of your subset.

In the subdocument API (lookupIn method) - I can use array index numbers to fetch particular elements. Is there a similar way to fetch array elements by value?

“fruits”: [
“apple”,
“pear”,
“banana”
]

fruits[0]=apple

Something like:
fruits[‘apple’]
fruits[a*]

If the array is long and I want to get a sublist based on values that may exist can I use the SDK to filter that list and not transfer the whole thing over the network.

We don’t support wildcard value fetches fruits[a*]. You’d have to use index to get an array element. To find if a value exists in the array, currently you would have to fetch the entire array and loop over it.