Couchbase SDK add unique items into subdocument


Hi I want to know if I can add items into a subdocument which does not exist in the subdocument yet.
I tried below code but it simply fails when an existing item is encountered. I just want to neglect these items.

Getting the subdocument and checking unique items is not a good option for me since the subdocument is really big.

.arrayAddUnique(“devices”, deviceEntities.toArray(), subdocOptionsBuilder)


The code you have is probably fine - just look at the result and if it’s “STATUS_PATH_EEXISTS” then just ignore it; only if it’s another “real” do you need to handle it.


I have solved this problem with N1QL instead of subdocument API. Bevause subdoument API prevents all operations if ever, a STATUS_PATH_EXISTS error arises.

I am leaving my DSL in case someone else searches for a solution:

 final Statement statement = update("bucketname")
                .set("devices", arrayPut("devices", "value1", "value2"));

This simply add value1 and value2 to devices attribute of the document with id “id” in. It automatically neglects already existing values. If value1 already exists in devices array, it will be skipped.


Sure, that’s the intent - the subdoc API is atomic - either all mutations (in a multi-mutation) are applied, or none are. You could consider having just a single path in your subdoc mutation.

N1QL is indeed more flexible, but you’ll likely find subdoc more performant. It depends on what the best trade-offs are for your particular workload.


Is there a way to instruct the API to ignore STATUS_PATH_EXISTS errors and proceed with other mutation operations.