code: def _merge_command(run, full_result, offset, result):
      
      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:
              n_upserted = len(upserted)
              for doc in upserted:
                  doc["index"] = run.index(doc["index"] + offset)
              full_result["upserted"].extend(upserted)
              full_result["nUpserted"] += n_upserted
              full_result["nMatched"] += (affected - n_upserted)
          else:
              full_result["nMatched"] += affected
          full_result["nModified"] += result["nModified"]
  
      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)
docs: Merge a write command result into the full bulk result.