(operation, command, result)
return res
Convert a legacy write result to write commmand format.
Convert a legacy write result to write commmand format.
Convert a legacy write result to write commmand format .
def _convert_write_result(operation, command, result):
    """Convert a legacy write result to write commmand format."""

    # Based on _merge_legacy from bulk.py
    affected = result.get("n", 0)
    res = {"ok": 1, "n": affected}
    errmsg = result.get("errmsg", result.get("err", ""))
    if errmsg:
        # The write was successful on at least the primary so don't return.
        if result.get("wtimeout"):
            res["writeConcernError"] = {"errmsg": errmsg,
                                        "code": 64,
                                        "errInfo": {"wtimeout": True}}
        else:
            # The write failed.
            error = {"index": 0,
                     "code": result.get("code", 8),
                     "errmsg": errmsg}
            if "errInfo" in result:
                error["errInfo"] = result["errInfo"]
            res["writeErrors"] = [error]
            return res
    if operation == "insert":
        # GLE result for insert is always 0 in most MongoDB versions.
        res["n"] = len(command['documents'])
    elif operation == "update":
        if "upserted" in result:
            res["upserted"] = [{"index": 0, "_id": result["upserted"]}]
        # Versions of MongoDB before 2.6 don't return the _id for an
        # upsert if _id is not an ObjectId.
        elif result.get("updatedExisting") is False and affected == 1:
            # If _id is in both the update document *and* the query spec
            # the update document _id takes precedence.
            update = command['updates'][0]
            _id = update["u"].get("_id", update["q"].get("_id"))
            res["upserted"] = [{"index": 0, "_id": _id}]
    return res
def _convert_write_result ( operation , command , result ) : # Based on _merge_legacy from bulk.py affected = result . get ( "n" , 0 ) res = { "ok" : 1 , "n" : affected } errmsg = result . get ( "errmsg" , result . get ( "err" , "" ) ) if errmsg : # The write was successful on at least the primary so don't return. if result . get ( "wtimeout" ) : res [ "writeConcernError" ] = { "errmsg" : errmsg , "code" : 64 , "errInfo" : { "wtimeout" : True } } else : # The write failed. error = { "index" : 0 , "code" : result . get ( "code" , 8 ) , "errmsg" : errmsg } if "errInfo" in result : error [ "errInfo" ] = result [ "errInfo" ] res [ "writeErrors" ] = [ error ] return res if operation == "insert" : # GLE result for insert is always 0 in most MongoDB versions. res [ "n" ] = len ( command [ 'documents' ] ) elif operation == "update" : if "upserted" in result : res [ "upserted" ] = [ { "index" : 0 , "_id" : result [ "upserted" ] } ] # Versions of MongoDB before 2.6 don't return the _id for an # upsert if _id is not an ObjectId. elif result . get ( "updatedExisting" ) is False and affected == 1 : # If _id is in both the update document *and* the query spec # the update document _id takes precedence. update = command [ 'updates' ] [ 0 ] _id = update [ "u" ] . get ( "_id" , update [ "q" ] . get ( "_id" ) ) res [ "upserted" ] = [ { "index" : 0 , "_id" : _id } ] return res