Couchbase MuteteInResult always returns Index 0 is invalid error

public boolean changeDataFetchOrder(String caseId, String taskId, boolean isRandom) {
    var searchId = "case" + ":" + caseId + ":" + taskId;
    Collection collection = bucket.defaultCollection();
    var result = collection.mutateIn(searchId, Collections.singletonList(
            upsert("isDataFetchOrderRandom", isRandom)
    return result.contentAs(0, Boolean.class);

This operation changes the field in database but it does not returns result of operation. Should I do an extra configuration?

com.couchbase.client.core.error.InvalidArgumentException: Index 0 is invalid
    at com.couchbase.client.core.error.InvalidArgumentException.fromMessage( ~[core-io-2.3.0.jar:na]
    at ~[java-client-3.3.0.jar:na]
    at ~[java-client-3.3.0.jar:na]
    at ~[java-client-3.3.0.jar:na]
    at ~[classes/:na]
    at ~[classes/:na]
    at ~[classes/:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke( ~[na:na]
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke( ~[na:na]

Hi @Erdem_Ontas

Yes, most mutateIn operations don’t return what they’ve written. This is because generally you know what that is - e.g. you have isRandom here - and it saves bytes on the wire.

The exception are counters: if you increment or decrement a counter, it does return what was written, since you otherwise wouldn’t that information.

So how can I be sure that the operation is successful?

Ah, you’re checking for a boolean result not to confirm what was written, but to check if the operation was successful? As with most SDK operations, the mutateIn will throw an exception if it fails. Please see these docs for details of the exceptions.

Thanks for explanation. Do you have any unit test example in Java. I am having problems with writing unit test to mutateIn

Sure - take a look here for mutateIn and here for lookupIn.

1 Like