def _handle_connection(connection): try: connection.send_message( message.update(self.full_collection_name, upsert, multi, spec, document, safe, kwargs), callback=callback) except: connection.close() raise
def update(self, spec, document, upsert=False, manipulate=False, safe=False, multi=False): """Update a document(s) in this collection. Raises :class:`TypeError` if either `spec` or `document` is not an instance of ``dict`` or `upsert` is not an instance of ``bool``. If `safe` is ``True`` then the update will be checked for errors, raising :class:`~pymongo.errors.OperationFailure` if one occurred. Safe inserts wait for a response from the database, while normal inserts do not. There are many useful `update modifiers`_ which can be used when performing updates. For example, here we use the ``"$set"`` modifier to modify some fields in a matching document: .. doctest:: >>> db.test.insert({"x": "y", "a": "b"}) ObjectId('...') >>> list(db.test.find()) [{u'a': u'b', u'x': u'y', u'_id': ObjectId('...')}] >>> db.test.update({"x": "y"}, {"$set": {"a": "c"}}) >>> list(db.test.find()) [{u'a': u'c', u'x': u'y', u'_id': ObjectId('...')}] :Parameters: - `spec`: a ``dict`` or :class:`~pymongo.son.SON` instance specifying elements which must be present for a document to be updated - `document`: a ``dict`` or :class:`~pymongo.son.SON` instance specifying the document to be used for the update or (in the case of an upsert) insert - see docs on MongoDB `update modifiers`_ - `upsert` (optional): perform an upsert operation - `manipulate` (optional): manipulate the document before updating? - `safe` (optional): check that the update succeeded? - `multi` (optional): update all documents that match `spec`, rather than just the first matching document. The default value for `multi` is currently ``False``, but this might eventually change to ``True``. It is recommended that you specify this argument explicitly for all update operations in order to prepare your code for that change. .. versionadded:: 1.1.1 The `multi` parameter. .. _update modifiers: http://www.mongodb.org/display/DOCS/Updating """ if not isinstance(spec, types.DictType): raise TypeError("spec must be an instance of dict") if not isinstance(document, types.DictType): raise TypeError("document must be an instance of dict") if not isinstance(upsert, types.BooleanType): raise TypeError("upsert must be an instance of bool") if upsert and manipulate: document = self.__database._fix_incoming(document, self) self.__database.connection._send_message( message.update(self.__full_name, upsert, multi, spec, document, safe), safe)
def update(self, spec, document, upsert=False, manipulate=False, safe=False, multi=False): """Update a single document in this collection. Raises TypeError if either spec or document isn't an instance of dict or upsert isn't an instance of bool. If `safe` is True then the update will be checked for errors, raising OperationFailure if one occurred. Safe inserts wait for a response from the database, while normal inserts do not. :Parameters: - `spec`: a SON object specifying elements which must be present for a document to be updated - `document`: a SON object specifying the document to be used for the update or (in the case of an upsert) insert. See docs on MongoDB `update modifiers <http://www.mongodb.org/display/DOCS/Updating>`_ - `upsert` (optional): perform an upsert operation - `manipulate` (optional): manipulate the document before updating? - `safe` (optional): check that the update succeeded? - `multi` (optional): update all documents that match `spec`, rather than just the first matching document. The default value for `multi` is currently False, but this might eventually change to True. It is recommended that you specify this argument explicitly for all update operations in order to prepare your code for that change. .. versionadded:: 1.1.1 The `multi` parameter. """ if not isinstance(spec, types.DictType): raise TypeError("spec must be an instance of dict") if not isinstance(document, types.DictType): raise TypeError("document must be an instance of dict") if not isinstance(upsert, types.BooleanType): raise TypeError("upsert must be an instance of bool") if upsert and manipulate: document = self.__database._fix_incoming(document, self) self.__database.connection()._send_message( message.update(self.full_name(), upsert, multi, spec, document, safe), safe)
def update(self, spec, document, upsert=False, manipulate=False, safe=True, multi=False, callback=None, **kwargs): """Update a document(s) in this collection. Raises :class:`TypeError` if either `spec` or `document` is not an instance of ``dict`` or `upsert` is not an instance of ``bool``. If `safe` is ``True`` then the update will be checked for errors, raising :class:`~pymongo.errors.OperationFailure` if one occurred. Safe updates require a response from the database, while normal updates do not - thus, setting `safe` to ``True`` will negatively impact performance. There are many useful `update modifiers`_ which can be used when performing updates. For example, here we use the ``"$set"`` modifier to modify some fields in a matching document: .. doctest:: >>> db.test.insert({"x": "y", "a": "b"}) ObjectId('...') >>> list(db.test.find()) [{u'a': u'b', u'x': u'y', u'_id': ObjectId('...')}] >>> db.test.update({"x": "y"}, {"$set": {"a": "c"}}) >>> list(db.test.find()) [{u'a': u'c', u'x': u'y', u'_id': ObjectId('...')}] If `safe` is ``True`` returns the response to the *lastError* command. Otherwise, returns ``None``. # Any additional keyword arguments imply ``safe=True``, and will # be used as options for the resultant `getLastError` # command. For example, to wait for replication to 3 nodes, pass # ``w=3``. :Parameters: - `spec`: a ``dict`` or :class:`~bson.son.SON` instance specifying elements which must be present for a document to be updated - `document`: a ``dict`` or :class:`~bson.son.SON` instance specifying the document to be used for the update or (in the case of an upsert) insert - see docs on MongoDB `update modifiers`_ - `upsert` (optional): perform an upsert if ``True`` - `manipulate` (optional): manipulate the document before updating? If ``True`` all instances of :mod:`~pymongo.son_manipulator.SONManipulator` added to this :class:`~pymongo.database.Database` will be applied to the document before performing the update. - `safe` (optional): check that the update succeeded? - `multi` (optional): update all documents that match `spec`, rather than just the first matching document. The default value for `multi` is currently ``False``, but this might eventually change to ``True``. It is recommended that you specify this argument explicitly for all update operations in order to prepare your code for that change. - `**kwargs` (optional): any additional arguments imply ``safe=True``, and will be used as options for the `getLastError` command .. _update modifiers: http://www.mongodb.org/display/DOCS/Updating .. mongodoc:: update """ if not isinstance(spec, dict): raise TypeError("spec must be an instance of dict") if not isinstance(document, dict): raise TypeError("document must be an instance of dict") if not isinstance(upsert, bool): raise TypeError("upsert must be an instance of bool") if not isinstance(safe, bool): raise TypeError("safe must be an instance of bool") # TODO: apply SON manipulators # if upsert and manipulate: # document = self.__database._fix_incoming(document, self) if kwargs: safe = True if safe and not callable(callback): raise TypeError("callback must be callable") if not safe and callback is not None: raise TypeError("callback can not be used with safe=False") if callback: callback = functools.partial(self._handle_response, orig_callback=callback) self.__limit = None connection = self.__pool.connection() try: connection.send_message( message.update(self.full_collection_name, upsert, multi, spec, document, safe, kwargs), callback=callback) except: connection.close() raise
def update(self, spec, document, upsert=False, manipulate=False, safe=False, multi=False): """Update a document(s) in this collection. Raises :class:`TypeError` if either `spec` or `document` is not an instance of ``dict`` or `upsert` is not an instance of ``bool``. If `safe` is ``True`` then the update will be checked for errors, raising :class:`~pymongo.errors.OperationFailure` if one occurred. Safe inserts wait for a response from the database, while normal inserts do not. There are many useful `update modifiers`_ which can be used when performing updates. For example, here we use the ``"$set"`` modifier to modify some fields in a matching document:: >>> db.test.insert({"x": "y", "a": "b"}) ObjectId('...') >>> list(db.test.find()) [{u'a': u'b', u'x': u'y', u'_id': ObjectId('...')}] >>> db.test.update({"x": "y"}, {"$set": {"a": "c"}}) >>> list(db.test.find()) [{u'a': u'c', u'x': u'y', u'_id': ObjectId('...')}] :Parameters: - `spec`: a ``dict`` or :class:`~pymongo.son.SON` instance specifying elements which must be present for a document to be updated - `document`: a ``dict`` or :class:`~pymongo.son.SON` instance specifying the document to be used for the update or (in the case of an upsert) insert - see docs on MongoDB `update modifiers`_ - `upsert` (optional): perform an upsert operation - `manipulate` (optional): manipulate the document before updating? - `safe` (optional): check that the update succeeded? - `multi` (optional): update all documents that match `spec`, rather than just the first matching document. The default value for `multi` is currently ``False``, but this might eventually change to ``True``. It is recommended that you specify this argument explicitly for all update operations in order to prepare your code for that change. .. versionadded:: 1.1.1 The `multi` parameter. .. _update modifiers: http://www.mongodb.org/display/DOCS/Updating """ if not isinstance(spec, types.DictType): raise TypeError("spec must be an instance of dict") if not isinstance(document, types.DictType): raise TypeError("document must be an instance of dict") if not isinstance(upsert, types.BooleanType): raise TypeError("upsert must be an instance of bool") if upsert and manipulate: document = self.__database._fix_incoming(document, self) self.__database.connection()._send_message( message.update(self.full_name(), upsert, multi, spec, document, safe), safe)