Array count using subdocument api (4.5)


I am trying to get the count of a nested array element. I know that this can be done easily using N1QL but that is not an option for performance reasons. While exploring the sub-document api, I came across AsyncLookupInBuilder.getCount but it seems to only be available in couchbase server 5.0 (we use 4.5). Aside from pulling back the entire document and processing it in memory, is there another way I can achieve this objective?



If you are using N1QL you can use the following option.

CREATE INDEX ix1 ON default(ARRAY_COUNT(arryelm));
SELECT ARRAY_COUNT(arryelm) FROM default WHERE ARRAY_COUNT(arryelm) >= 0

Hi @vsr1,

Unfortunately I cannot use N1QL as we are seeing some performance issues with it.

As you identified, getCount() was added in 5.0. If you can’t upgrade (and need to use K/V) you’ll have to pull the document down and parse it yourself.

One alternative would be to define a map/reduce view which calculates the number of elements in your nester array.

@k_reid can you please share the performance issues you are seeing with N1QL? We can figure out what is going on