'Update and return the updated document.
Returns either the document before or after modification based on `new`
parameter. If no documents match the query and `upsert` is false,
returns ``None``. If upserting and `new` is false, returns ``None``.
If the full_response parameter is ``True``, the return value will be
the entire response object from the server, including the \'ok\' and
\'lastErrorObject\' fields, rather than just the modified document.
This is useful mainly because the \'lastErrorObject\' document holds
information about the command\'s execution.
:param upsert: insert if document doesn\'t exist (default ``False``)
:param full_response: return the entire response object from the
server (default ``False``, not available for PyMongo 3+)
:param remove: remove rather than updating (default ``False``)
:param new: return updated rather than original document
(default ``False``)
:param update: Django-style update keyword arguments
.. versionadded:: 0.9'

def modify(self, upsert=False, full_response=False, remove=False, new=False, **update):

    if (remove and new):
        raise OperationError('Conflicting    parameters:    remove    and    new')
    if ((not update) and (not upsert) and (not remove)):
        raise OperationError('No    update    parameters,    must    either    update    or    remove')
    queryset = self.clone()
    query = queryset._query
    if ((not IS_PYMONGO_3) or (not remove)):
        update = transform.update(queryset._document, **update)
    sort = queryset._ordering
    try:
        if IS_PYMONGO_3:
            if full_response:
                msg = 'With    PyMongo    3+,    it    is    not    possible    anymore    to    get    the    full    response.'
                warnings.warn(msg, DeprecationWarning)
            if remove:
                result = queryset._collection.find_one_and_delete(query, sort=sort, **self._cursor_args)
            else:
                if new:
                    return_doc = ReturnDocument.AFTER
                else:
                    return_doc = ReturnDocument.BEFORE
                result = queryset._collection.find_one_and_update(query, update, upsert=upsert, sort=sort, return_document=return_doc, **self._cursor_args)
        else:
            result = queryset._collection.find_and_modify(query, update, upsert=upsert, sort=sort, remove=remove, new=new, full_response=full_response, **self._cursor_args)
    except pymongo.errors.DuplicateKeyError as err:
        raise NotUniqueError((u'Update    failed    (%s)' % err))
    except pymongo.errors.OperationFailure as err:
        raise OperationError((u'Update    failed    (%s)' % err))
    if full_response:
        if (result['value'] is not None):
            result['value'] = self._document._from_son(result['value'], only_fields=self.only_fields)
    elif (result is not None):
        result = self._document._from_son(result, only_fields=self.only_fields)
    return result
