MutationResult to String and Object

Hi,

I’m in-progress of converting SDK 2 code into SDK 3.

How to convert MutationResult into Optional<Map<String,Object>> ?

Cannot resolve method ‘content’ in ‘MutationResult’

 @Override
    public Map<String, Object> upsertDocument(final String identifier, final int expiry, Map<String, Object> data) {
        return Optional.ofNullable(upsertCouchbaseDocument(identifier, expiry, data))
                .map(d -> gson.fromJson(d.content(),Map.class))
                .orElse(Collections.EMPTY_MAP);
    }
private MutationResult upsertCouchbaseDocument(final String identifier, final int expiry, final Map<String, Object> data) {
        try{
            return couchbaseConfiguration.getBucket().defaultCollection().upsert(identifier,data,UpsertOptions.upsertOptions().expiry(Duration.ofSeconds(expiry)));
        } catch (RuntimeException rte) {
            throw new DataAccessException(rte.getMessage(), rte.getCause());
        }

Thanks.

Hi Strike, welcome to the forum!

A MutationResult does not have the document content. It just has a cas value and a mutation token.

Do you want your upsertDocument method to return the content of the updated document? If so, is there a reason you can’t just return the data argument?

Thanks,
David

Hi David,

I can return the data but how do we find if data has been successfully inserted into Couchbase?

Are there any other solution other than MutationResult?

Thanks.

how do we find if data has been successfully inserted into Couchbase?

If upsert (or insert, or replace, etc.) does not throw an exception, then the mutation was successful.