(run, full_result, results)
Merge a group of results from write commands into the full result.
Merge a group of results from write commands into the full result.
Merge a group of results from write commands into the full result .
def _merge_command(run, full_result, results):
    """Merge a group of results from write commands into the full result.
    """
    for offset, result in results:

        affected = result.get("n", 0)

        if run.op_type == _INSERT:
            full_result["nInserted"] += affected

        elif run.op_type == _DELETE:
            full_result["nRemoved"] += affected

        elif run.op_type == _UPDATE:
            upserted = result.get("upserted")
            if upserted:
                if isinstance(upserted, list):
                    n_upserted = len(upserted)
                    for doc in upserted:
                        doc["index"] = run.index(doc["index"] + offset)
                    full_result["upserted"].extend(upserted)
                else:
                    n_upserted = 1
                    index = run.index(offset)
                    doc = {_UINDEX: index, _UID: upserted}
                    full_result["upserted"].append(doc)
                full_result["nUpserted"] += n_upserted
                full_result["nMatched"] += (affected - n_upserted)
            else:
                full_result["nMatched"] += affected
            n_modified = result.get("nModified")
            # SERVER-13001 - in a mixed sharded cluster a call to
            # update could return nModified (>= 2.6) or not (<= 2.4).
            # If any call does not return nModified we can't report
            # a valid final count so omit the field completely.
            if n_modified is not None and "nModified" in full_result:
                full_result["nModified"] += n_modified
            else:
                full_result.pop("nModified", None)

        write_errors = result.get("writeErrors")
        if write_errors:
            for doc in write_errors:
                # Leave the server response intact for APM.
                replacement = doc.copy()
                idx = doc["index"] + offset
                replacement["index"] = run.index(idx)
                # Add the failed operation to the error document.
                replacement[_UOP] = run.ops[idx]
                full_result["writeErrors"].append(replacement)

        wc_error = result.get("writeConcernError")
        if wc_error:
            full_result["writeConcernErrors"].append(wc_error)
def _merge_command ( run , full_result , results ) : for offset , result in results : affected = result . get ( "n" , 0 ) if run . op_type == _INSERT : full_result [ "nInserted" ] += affected elif run . op_type == _DELETE : full_result [ "nRemoved" ] += affected elif run . op_type == _UPDATE : upserted = result . get ( "upserted" ) if upserted : if isinstance ( upserted , list ) : n_upserted = len ( upserted ) for doc in upserted : doc [ "index" ] = run . index ( doc [ "index" ] + offset ) full_result [ "upserted" ] . extend ( upserted ) else : n_upserted = 1 index = run . index ( offset ) doc = { _UINDEX : index , _UID : upserted } full_result [ "upserted" ] . append ( doc ) full_result [ "nUpserted" ] += n_upserted full_result [ "nMatched" ] += ( affected - n_upserted ) else : full_result [ "nMatched" ] += affected n_modified = result . get ( "nModified" ) # SERVER-13001 - in a mixed sharded cluster a call to # update could return nModified (>= 2.6) or not (<= 2.4). # If any call does not return nModified we can't report # a valid final count so omit the field completely. if n_modified is not None and "nModified" in full_result : full_result [ "nModified" ] += n_modified else : full_result . pop ( "nModified" , None ) write_errors = result . get ( "writeErrors" ) if write_errors : for doc in write_errors : # Leave the server response intact for APM. replacement = doc . copy ( ) idx = doc [ "index" ] + offset replacement [ "index" ] = run . index ( idx ) # Add the failed operation to the error document. replacement [ _UOP ] = run . ops [ idx ] full_result [ "writeErrors" ] . append ( replacement ) wc_error = result . get ( "writeConcernError" ) if wc_error : full_result [ "writeConcernErrors" ] . append ( wc_error )