def __find_and_modify(self, filter, projection, sort, upsert=None,
                          return_document=ReturnDocument.BEFORE,
                          array_filters=None, session=None, **kwargs):
        """Internal findAndModify helper."""

        common.validate_is_mapping("filter", filter)
        if not isinstance(return_document, bool):
            raise ValueError("return_document must be "
                             "ReturnDocument.BEFORE or ReturnDocument.AFTER")
        collation = validate_collation_or_none(kwargs.pop('collation', None))
        cmd = SON([("findAndModify", self.__name),
                   ("query", filter),
                   ("new", return_document)])
        cmd.update(kwargs)
        if projection is not None:
            cmd["fields"] = helpers._fields_list_to_dict(projection,
                                                         "projection")
        if sort is not None:
            cmd["sort"] = helpers._index_document(sort)
        if upsert is not None:
            common.validate_boolean("upsert", upsert)
            cmd["upsert"] = upsert

        write_concern = self._write_concern_for_cmd(cmd, session)

        def _find_and_modify(session, sock_info, retryable_write):
            if array_filters is not None:
                if sock_info.max_wire_version < 6:
                    raise ConfigurationError(
                        'Must be connected to MongoDB 3.6+ to use '
                        'arrayFilters.')
                if not write_concern.acknowledged:
                    raise ConfigurationError(
                        'arrayFilters is unsupported for unacknowledged '
                        'writes.')
                cmd["arrayFilters"] = array_filters
            if (sock_info.max_wire_version >= 4 and
                    not write_concern.is_server_default):
                cmd['writeConcern'] = write_concern.document
            out = self._command(sock_info, cmd,
                                read_preference=ReadPreference.PRIMARY,
                                write_concern=write_concern,
                                allowable_errors=[_NO_OBJ_ERROR],
                                collation=collation, session=session,
                                retryable_write=retryable_write,
                                user_fields=_FIND_AND_MODIFY_DOC_FIELDS)
            _check_write_command_response(out)

            return out.get("value")

        return self.__database.client._retryable_write(
            write_concern.acknowledged, _find_and_modify, session)
def __find_and_modify ( self , filter , projection , sort , upsert = None , return_document = ReturnDocument . BEFORE , array_filters = None , session = None , ** kwargs ) : common . validate_is_mapping ( "filter" , filter ) if not isinstance ( return_document , bool ) : raise ValueError ( "return_document must be " "ReturnDocument.BEFORE or ReturnDocument.AFTER" ) collation = validate_collation_or_none ( kwargs . pop ( 'collation' , None ) ) cmd = SON ( [ ( "findAndModify" , self . __name ) , ( "query" , filter ) , ( "new" , return_document ) ] ) cmd . update ( kwargs ) if projection is not None : cmd [ "fields" ] = helpers . _fields_list_to_dict ( projection , "projection" ) if sort is not None : cmd [ "sort" ] = helpers . _index_document ( sort ) if upsert is not None : common . validate_boolean ( "upsert" , upsert ) cmd [ "upsert" ] = upsert write_concern = self . _write_concern_for_cmd ( cmd , session ) def _find_and_modify ( session , sock_info , retryable_write ) : if array_filters is not None : if sock_info . max_wire_version < 6 : raise ConfigurationError ( 'Must be connected to MongoDB 3.6+ to use ' 'arrayFilters.' ) if not write_concern . acknowledged : raise ConfigurationError ( 'arrayFilters is unsupported for unacknowledged ' 'writes.' ) cmd [ "arrayFilters" ] = array_filters if ( sock_info . max_wire_version >= 4 and not write_concern . is_server_default ) : cmd [ 'writeConcern' ] = write_concern . document out = self . _command ( sock_info , cmd , read_preference = ReadPreference . PRIMARY , write_concern = write_concern , allowable_errors = [ _NO_OBJ_ERROR ] , collation = collation , session = session , retryable_write = retryable_write , user_fields = _FIND_AND_MODIFY_DOC_FIELDS ) _check_write_command_response ( out ) return out . get ( "value" ) return self . __database . client . _retryable_write ( write_concern . acknowledged , _find_and_modify , session )
Internal findAndModify helper.