def modify(self, update, query=None): """Modify this document using :meth:`find_and_modify`. If the document could not be updated a :class:`~scalymongo.errors.ModifyFailedError` is raised. This is usually caused by the document not being found on the server (e.g. it was deleted or does not match the specified `query`). :param update: Is an update modifier or replacement document to be be used for this :param query: A query specification for any additional parameters to be used in the :meth:`find_and_modify` query. This document's ``_id`` field and it's shard key fields (where applicable) are included in addition to any parameters specified in the `query`. """ full_query = self.shard_key if query: full_query.update(query) full_query['_id'] = self['_id'] result = self.find_and_modify(full_query, update, new=True) if result is None: self.reload() raise ModifyFailedError( 'Failed to update document. The document was not found' ' based on the criteria {0}. Document was {1}.'.format( query, self), ) # We have to clear the existing dictionary first in case the operation # included an `$unset` or replaced the entire document. self.clear() # Call the parent `update` not the classmethod. SchemaDocument.update(self, result)
def reload(self): """Reload this document. This will make use of the shard key for sharded collections to avoid a global query. """ spec = self.shard_key spec['_id'] = self['_id'] # Call the parent `update` not the classmethod. SchemaDocument.update(self, self.find_one(spec))
def modify(self, update, query=None): """Modify this document using :meth:`find_and_modify`. :param update: Is an update modifier or replacement document to be be used for this :param query: A query specification for any additional parameters to be used in the :meth:`find_and_modify` query. This document's ``_id`` field and it's shard key fields (where applicable) are included in addition to any parameters specified in the `query`. """ full_query = self.shard_key if query: full_query.update(query) full_query['_id'] = self['_id'] result = self.find_and_modify(full_query, update, new=True) # We have to clear the existing dictionary first in case the operation # included an `$unset` or replaced the entire document. self.clear() # Call the parent `update` not the classmethod. SchemaDocument.update(self, result)