How to process upsert_multi result(MultiMutationResult)

Hi,

Versions

  • Couchbase Server 7.2
  • Python
  • couchbase==4.1.2(python package)

Context
Referencing the Python SDK for bulk operations I’m able to upload to a bucket using the function below.

# Bulk operation -- upsert
upsert_multi_result = cb_coll.upsert_multi(dict)

Question
How can I process the output of the result in the form “MultiMutationResult” below to determine there were any failures. Refer to Docs

# Result of upsert_multi_result 
MultiMutationResult( 39515:MutationResult:result:{err=0, err_string=Success, value={'cas': 1697541814678978560, 'key': '11111', 'mutation_token': <pycbc_core.mutation_token object at 0x7fbd94529c30>}}, 39485:MutationResult:result:{err=0, err_string=Success, value={'cas': 1697541814679109632, 'key': '22222', 'mutation_token': <pycbc_core.mutation_token object at 0x7fbd94308e10>}}, 39525:MutationResult:result:{err=0, err_string=Success, value={'cas': 1697541814677995520, 'key': '3333', 'mutation_token': <pycbc_core.mutation_token object at 0x7fbd9430bbb0>}}, 39752:MutationResult:result:{err=0, err_string=Success, value={'cas': 1697541814680485888, 'key': '44444', 'mutation_token': <pycbc_core.mutation_token object at 0x7fbd9430bf50>}})

I’ve used the following

# Error handling
if upsert_multi_result.results:
    print(f"True if all bulk upserts succeeded, false otherwise: {upsert_multi_result.all_ok}")
else:
    for couchbase_multi_upser_keys, couchbase_multi_upsert_exceptions in upsert_multi_result.exceptions.items():
        print(f"{couchbase_multi_upser_keys} : {couchbase_multi_upsert_exceptions}")

Hi @ii – I would like to caution you on using MultiMutaitonResult’s results property as your check for if all the upsert operations succeeded from your multi upsert operation. Using if upsert_multi_result.results will be true if the results property is a non-empty dict (so it will be true if only a single operation succeeded an the rest failed). A better check would be to use the all_okay property or verify that every result in the results dict is a MutationResult: all(map(lambda r: isinstance(r, MutationResult), res.results.values()))

Hi @jcasey,
Updated function below.

def upsert_records(cb_coll: Collection, records: Dict[str, dict]) -> Optional[Dict[str, Exception]]:
    """
    Upsert multiple records into a Couchbase collection.

    Args:
        cb_coll (couchbase.collection.Collection): The Couchbase collection where records will be upserted.
        records (Dict[str, dict]): A dictionary of records to upsert, where the keys are record identifiers.

    Returns:
        Optional[Dict[str, Exception]]: A dictionary of exceptions for records that failed to upsert, or None if all records were upserted successfully.
    """

    upsert_multi_result = cb_coll.upsert_multi(records)
    if not upsert_multi_result.all_ok:
        return upsert_multi_result.exceptions
    return None

Leave a response above is the recommended way.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.