Ejemplo n.º 1
0
    def update(self, spec, document, upsert=False, safe=True,
        multi=False, callback=None):
        """Update a document(s) in this collection.

        :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``
          - `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.
        """
        if not isinstance(spec, dict):
            spec = {'_id': spec}
        assert isinstance(document, dict), "document must be an instance of dict"
        assert isinstance(upsert, bool), "upsert must be an instance of bool"
        assert isinstance(safe, bool), "safe must be an instance of bool"

        msg = message.update(self._collection_name, upsert,
                             multi, spec, document, safe, {})
        log.debug("mongo: db.{0}.update({1}, {2}, {3}, {4})".format(
            self._collection_name, spec, document, upsert, multi))

        node = self._database.get_node(ReadPreference.PRIMARY)
        node.connection(lambda conn: conn.send_message(msg, safe, False, callback))
Ejemplo n.º 2
0
    def update(self, document=None, safe=True, callback=None):
        """Update a document

        :Parameters:
        - `safe` (optional): safe update operation
        - `callback` : method which will be called when update is finished
        """
        pre_update.send(instance=self)

        if not document:
            document = self.as_dict()

        database = Database()
        collection_name = database.get_collection_name(self.__collection__)
        spec = {'_id': self._id}

        message_update = message.update(collection_name, False,
                False, spec, document, safe, {})

        response, error = yield gen.Task(database.send_message, message_update)

        post_update.send(instance=self)

        if callback:
            callback((response, error))
Ejemplo n.º 3
0
    def update(self, spec, document, upsert=False, safe=True,
        multi=False, callback=None):
        """Update a document(s) in this collection.

        :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``
          - `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.
        """
        assert isinstance(spec, dict), "spec must be an instance of dict"
        assert isinstance(document, dict), "document must be an instance of dict"
        assert isinstance(upsert, bool), "upsert must be an instance of bool"
        assert isinstance(safe, bool), "safe must be an instance of bool"

        message_update = message.update(self._collection_name, upsert,
                multi, spec, document, safe, {})

        response, error = yield gen.Task(self._database.send_message,
            message_update, read_preference=ReadPreference.PRIMARY)

        callback((response, error))
Ejemplo n.º 4
0
    def update(self,
               spec,
               document,
               upsert=False,
               safe=True,
               multi=False,
               callback=None):
        """Update a document(s) in this collection.

        :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``
          - `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.
        """
        assert isinstance(spec, dict), "spec must be an instance of dict"
        assert isinstance(document,
                          dict), "document must be an instance of dict"
        assert isinstance(upsert, bool), "upsert must be an instance of bool"
        assert isinstance(safe, bool), "safe must be an instance of bool"

        message_update = message.update(self._collection_name, upsert, multi,
                                        spec, document, safe, {})

        log.debug("mongo: db.{0}.update({1}, {2}, {3}, {4})".format(
            self._collection_name, spec, document, upsert, multi))

        node = yield gen.Task(self._database.get_node, ReadPreference.PRIMARY)
        connection = yield gen.Task(node.connection)

        response, error = yield gen.Task(connection.send_message,
                                         message_update, safe)

        callback((response, error))