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(InvalidArgumentException.java:28) ~[core-io-2.3.0.jar:na]
    at com.couchbase.client.java.kv.MutateInResult.getFieldAtIndex(MutateInResult.java:118) ~[java-client-3.3.0.jar:na]
    at com.couchbase.client.java.kv.MutateInResult.contentAs(MutateInResult.java:91) ~[java-client-3.3.0.jar:na]
    at com.couchbase.client.java.kv.MutateInResult.contentAs(MutateInResult.java:68) ~[java-client-3.3.0.jar:na]
    at com.company.caseuploadapi.service.couchbase.CaseRepositoryImpl.changeDataFetchOrder(CaseRepositoryImpl.java:179) ~[classes/:na]
    at com.company.caseuploadapi.service.cases.defaults.CaseService.setDataFetchOrder(CaseService.java:371) ~[classes/:na]
    at com.company.caseuploadapi.controller.CaseController.setDataFetchOrder(CaseController.java:79) ~[classes/:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64) ~[na:na]
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[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