def remove(self, spec_or_id=None, safe=False, **kwargs):
        """Remove a document(s) from this collection.

        .. warning:: Calls to :meth:`remove` should be performed with
           care, as removed data cannot be restored.

        If `safe` is ``True`` then the remove operation will be
        checked for errors, raising
        :class:`~pymongo.errors.OperationFailure` if one
        occurred. Safe removes wait for a response from the database,
        while normal removes do not.

        If `spec_or_id` is ``None``, all documents in this collection
        will be removed. This is not equivalent to calling
        :meth:`~pymongo.database.Database.drop_collection`, however,
        as indexes will not be removed.

        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_or_id` (optional): a dictionary specifying the
            documents to be removed OR any other type specifying the
            value of ``"_id"`` for the document to be removed
          - `safe` (optional): check that the remove succeeded?
          - `**kwargs` (optional): any additional arguments imply
            ``safe=True``, and will be used as options for the
            `getLastError` command

        .. versionadded:: 1.8
           Support for passing `getLastError` options as keyword arguments.
        .. versionchanged:: 1.7 Accept any type other than a ``dict``
           instance for removal by ``"_id"``, not just
           :class:`~bson.objectid.ObjectId` instances.
        .. versionchanged:: 1.4
           Return the response to *lastError* if `safe` is ``True``.
        .. versionchanged:: 1.2
           The `spec_or_id` parameter is now optional. If it is
           not specified *all* documents in the collection will be
           removed.
        .. versionadded:: 1.1
           The `safe` parameter.

        .. mongodoc:: remove
        """
        if spec_or_id is None:
            spec_or_id = {}
        if not isinstance(spec_or_id, dict):
            spec_or_id = {"_id": spec_or_id}

        if kwargs:
            safe = True

        return self.__database.connection._send_message(
            message.delete(self.__full_name, spec_or_id, safe, kwargs), safe)
示例#2
0
    def remove(self, spec_or_id=None, safe=False, **kwargs):
        """Remove a document(s) from this collection.

        .. warning:: Calls to :meth:`remove` should be performed with
           care, as removed data cannot be restored.

        If `safe` is ``True`` then the remove operation will be
        checked for errors, raising
        :class:`~pymongo.errors.OperationFailure` if one
        occurred. Safe removes wait for a response from the database,
        while normal removes do not.

        If `spec_or_id` is ``None``, all documents in this collection
        will be removed. This is not equivalent to calling
        :meth:`~pymongo.database.Database.drop_collection`, however,
        as indexes will not be removed.

        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_or_id` (optional): a dictionary specifying the
            documents to be removed OR any other type specifying the
            value of ``"_id"`` for the document to be removed
          - `safe` (optional): check that the remove succeeded?
          - `**kwargs` (optional): any additional arguments imply
            ``safe=True``, and will be used as options for the
            `getLastError` command

        .. versionadded:: 1.8
           Support for passing `getLastError` options as keyword arguments.
        .. versionchanged:: 1.7 Accept any type other than a ``dict``
           instance for removal by ``"_id"``, not just
           :class:`~bson.objectid.ObjectId` instances.
        .. versionchanged:: 1.4
           Return the response to *lastError* if `safe` is ``True``.
        .. versionchanged:: 1.2
           The `spec_or_id` parameter is now optional. If it is
           not specified *all* documents in the collection will be
           removed.
        .. versionadded:: 1.1
           The `safe` parameter.

        .. mongodoc:: remove
        """
        if spec_or_id is None:
            spec_or_id = {}
        if not isinstance(spec_or_id, dict):
            spec_or_id = {"_id": spec_or_id}

        if kwargs:
            safe = True

        return self.__database.connection._send_message(
            message.delete(self.__full_name, spec_or_id, safe, kwargs), safe)
示例#3
0
    def remove(self, spec_or_object_id=None, safe=False):
        """Remove a document(s) from this collection.

        .. warning:: Calls to :meth:`remove` should be performed with
           care, as removed data cannot be restored.

        Raises :class:`~pymongo.errors.TypeError` if
        `spec_or_object_id` is not an instance of (``dict``,
        :class:`~pymongo.objectid.ObjectId`). If `safe` is ``True``
        then the remove operation will be checked for errors, raising
        :class:`~pymongo.errors.OperationFailure` if one
        occurred. Safe removes wait for a response from the database,
        while normal removes do not.

        If no `spec_or_object_id` is given all documents in this
        collection will be removed. This is not equivalent to calling
        :meth:`~pymongo.database.Database.drop_collection`, however, as
        indexes will not be removed.

        If `safe` is ``True`` returns the response to the *lastError*
        command. Otherwise, returns ``None``.

        :Parameters:
          - `spec_or_object_id` (optional): a ``dict`` or
            :class:`~pymongo.son.SON` instance specifying which documents
            should be removed; or an instance of
            :class:`~pymongo.objectid.ObjectId` specifying the value of the
            ``_id`` field for the document to be removed
          - `safe` (optional): check that the remove succeeded?

        .. versionchanged:: 1.4
           Return the response to *lastError* if `safe` is ``True``.
        .. versionchanged:: 1.2
           The `spec_or_object_id` parameter is now optional. If it is
           not specified *all* documents in the collection will be
           removed.
        .. versionadded:: 1.1
           The `safe` parameter.

        .. mongodoc:: remove
        """
        spec = spec_or_object_id
        if spec is None:
            spec = {}
        if isinstance(spec, ObjectId):
            spec = {"_id": spec}

        if not isinstance(spec, dict):
            raise TypeError("spec must be an instance of dict, not %s" %
                            type(spec))

        return self.__database.connection._send_message(
            message.delete(self.__full_name, spec, safe), safe)
示例#4
0
文件: collection.py 项目: isdb/idb.py
    def remove(self, spec_or_object_id=None, safe=False):
        """Remove a document(s) from this collection.

        .. warning:: Calls to :meth:`remove` should be performed with
           care, as removed data cannot be restored.

        Raises :class:`~pymongo.errors.TypeError` if
        `spec_or_object_id` is not an instance of (``dict``,
        :class:`~pymongo.objectid.ObjectId`). If `safe` is ``True``
        then the remove operation will be checked for errors, raising
        :class:`~pymongo.errors.OperationFailure` if one
        occurred. Safe removes wait for a response from the database,
        while normal removes do not.

        If no `spec_or_object_id` is given all documents in this
        collection will be removed. This is not equivalent to calling
        :meth:`~pymongo.database.Database.drop_collection`, however, as
        indexes will not be removed.

        If `safe` is ``True`` returns the response to the *lastError*
        command. Otherwise, returns ``None``.

        :Parameters:
          - `spec_or_object_id` (optional): a ``dict`` or
            :class:`~pymongo.son.SON` instance specifying which documents
            should be removed; or an instance of
            :class:`~pymongo.objectid.ObjectId` specifying the value of the
            ``_id`` field for the document to be removed
          - `safe` (optional): check that the remove succeeded?

        .. versionchanged:: 1.4
           Return the response to *lastError* if `safe` is ``True``.
        .. versionchanged:: 1.2
           The `spec_or_object_id` parameter is now optional. If it is
           not specified *all* documents in the collection will be
           removed.
        .. versionadded:: 1.1
           The `safe` parameter.

        .. mongodoc:: remove
        """
        spec = spec_or_object_id
        if spec is None:
            spec = {}
        if isinstance(spec, ObjectId):
            spec = {"_id": spec}

        if not isinstance(spec, dict):
            raise TypeError("spec must be an instance of dict, not %s" %
                            type(spec))

        return self.__database.connection._send_message(
            message.delete(self.__full_name, spec, safe), safe)
示例#5
0
def test_pymongo():
    response = b"%s%s%s%s%s" % (struct.pack("<i", 4), struct.pack(
        "<q", 1), struct.pack("<i", 1), struct.pack(
            "<i", 1), bson.BSON.encode({"hello": "world"}))
    assert asynmongo._unpack_response(response, 1) == {
        'starting_from': 1,
        'number_returned': 1,
        'cursor_id': 1,
        'data': [{
            'hello': 'world'
        }]
    }

    payload = bson.BSON.encode({"hello": "world"})
    response = b"%s%s%s" % (struct.pack(
        "<B", 0), struct.pack("<i", len(payload)), payload)
    assert asynmongo._unpack_response(response, 2013) == {
        'first_payload_type': 0,
        'data': [{
            'hello': 'world'
        }],
        'first_payload_size': 22
    }

    opts = CodecOptions(SON)
    assert auth._auth_key(1, "a", "b") == "90b38d5dbfabd0b883e17ae67847220a"
    assert message.query(0, "%s.$cmd" % "mydb", 0, 1, SON({
        'getnonce': 1
    }), SON({}), opts) == (
        1804289383,
        b'>\x00\x00\x00gE\x8bk\x00\x00\x00\x00\xd4\x07\x00\x00\x00\x00\x00\x00mydb.$cmd\x00\x00\x00\x00\x00\x01\x00\x00\x00\x13\x00\x00\x00\x10getnonce\x00\x01\x00\x00\x00\x00\x05\x00\x00\x00\x00',
        19)
    assert message.query(0, "col.a", 0, 1, {"_id": 1}, None, opts) == (
        846930886,
        b'0\x00\x00\x00\xc6#{2\x00\x00\x00\x00\xd4\x07\x00\x00\x00\x00\x00\x00col.a\x00\x00\x00\x00\x00\x01\x00\x00\x00\x0e\x00\x00\x00\x10_id\x00\x01\x00\x00\x00\x00',
        14)
    assert message.update("col.a", 0, 0, {"_id": 1}, {
        "a": 1
    }, 1, (), False, opts) == (
        1681692777,
        b'8\x00\x00\x00i\x98<d\x00\x00\x00\x00\xd1\x07\x00\x00\x00\x00\x00\x00col.a\x00\x00\x00\x00\x00\x0e\x00\x00\x00\x10_id\x00\x01\x00\x00\x00\x00\x0c\x00\x00\x00\x10a\x00\x01\x00\x00\x00\x00<\x00\x00\x00i\x98<d\x00\x00\x00\x00\xd4\x07\x00\x00\x00\x00\x00\x00col.$cmd\x00\x00\x00\x00\x00\xff\xff\xff\xff\x17\x00\x00\x00\x10getlasterror\x00\x01\x00\x00\x00\x00',
        14)

    msg = message.delete("col.a", {"_id": 1}, 1, (), opts, 0)
    assert len(msg) == 3 and msg[0] == 1714636915 and msg[-1] == 14

    msg = message.insert("col.a", [{"a": 1}], False, 1, (), 0, opts)
    assert len(msg) == 3 and msg[0] == 1957747793 and msg[-1] == 12