Example #1
0
    def insert_many(self, documents, ordered=True, **kwargs):
        """Insert an iterable of documents into collection

        :param documents:
            An iterable of documents to insert (``list``,
            ``tuple``, ...)

        :param ordered:
            If ``True`` (the default) documents will be inserted on the server
            serially, in the order provided. If an error occurs, all remaining
            inserts are aborted. If ``False``, documents will be inserted on
            the server in arbitrary order, possibly in parallel, and all
            document inserts will be attempted.

        :returns:
            :class:`Deferred` that called back with
            :class:`pymongo.results.InsertManyResult`
        """
        inserted_ids = []
        for doc in documents:
            if isinstance(doc, collections.Mapping):
                inserted_ids.append(doc.setdefault("_id", ObjectId()))
            else:
                raise TypeError("TxMongo: insert_many takes list of documents.")

        bulk = _Bulk(self, ordered, bypass_document_validation=False)
        bulk.ops = [(_INSERT, doc) for doc in documents]
        yield self._execute_bulk(bulk)
        defer.returnValue(InsertManyResult(inserted_ids, self.write_concern.acknowledged))
Example #2
0
    def insert_many(self, documents, ordered=True, **kwargs):
        """Insert an iterable of documents into collection

        :param documents:
            An iterable of documents to insert (``list``,
            ``tuple``, ...)

        :param ordered:
            If ``True`` (the default) documents will be inserted on the server
            serially, in the order provided. If an error occurs, all remaining
            inserts are aborted. If ``False``, documents will be inserted on
            the server in arbitrary order, possibly in parallel, and all
            document inserts will be attempted.

        :returns:
            :class:`Deferred` that called back with
            :class:`pymongo.results.InsertManyResult`
        """
        inserted_ids = []
        for doc in documents:
            if isinstance(doc, collections.Mapping):
                inserted_ids.append(doc.setdefault("_id", ObjectId()))
            else:
                raise TypeError(
                    "TxMongo: insert_many takes list of documents.")

        bulk = _Bulk(self, ordered, bypass_document_validation=False)
        bulk.ops = [(_INSERT, doc) for doc in documents]
        yield self._execute_bulk(bulk)
        defer.returnValue(
            InsertManyResult(inserted_ids, self.write_concern.acknowledged))
Example #3
0
    def bulk_write(self, requests, ordered=True):
        if not isinstance(requests, collections.Iterable):
            raise TypeError("requests must be iterable")

        requests = list(requests)

        blk = _Bulk(self, ordered, bypass_document_validation=False)
        for request in requests:
            if not isinstance(request, _WriteOp):
                raise TypeError("{} is not a valid request".format(request))
            request._add_to_bulk(blk)

        return self._execute_bulk(blk)
Example #4
0
    def bulk_write(self, requests, ordered=True):
        if not isinstance(requests, collections.Iterable):
            raise TypeError("requests must be iterable")

        requests = list(requests)

        blk = _Bulk(self, ordered, bypass_document_validation=False)
        for request in requests:
            if not isinstance(request, _WriteOp):
                raise TypeError("{} is not a valid request".format(request))
            request._add_to_bulk(blk)

        return self._execute_bulk(blk)
Example #5
0
    def _insert(self,
                docs,
                ordered=True,
                check_keys=True,
                manipulate=False,
                write_concern=None,
                op_id=None,
                bypass_doc_val=False,
                session=None):
        """Internal insert helper."""
        if isinstance(docs, abc.Mapping):
            return self._insert_one(docs)

        ids = []

        if manipulate:

            def gen():
                """Generator that applies SON manipulators to each document
                and adds _id if necessary.
                """
                _db = self.__database
                for doc in docs:
                    # Apply user-configured SON manipulators. This order of
                    # operations is required for backwards compatibility,
                    # see PYTHON-709.
                    doc = _db._apply_incoming_manipulators(doc, self)
                    if not (isinstance(doc, RawBSONDocument) or '_id' in doc):
                        doc['_id'] = ObjectId()

                    doc = _db._apply_incoming_copying_manipulators(doc, self)
                    ids.append(doc['_id'])
                    yield doc
        else:

            def gen():
                """Generator that only tracks existing _ids."""
                for doc in docs:
                    # Don't inflate RawBSONDocument by touching fields.
                    if not isinstance(doc, RawBSONDocument):
                        ids.append(doc.get('_id'))
                    yield doc

        write_concern = write_concern or self._write_concern_for(session)
        blk = _Bulk(self, ordered, bypass_doc_val)
        blk.ops = [(message._INSERT, doc) for doc in gen()]
        try:
            blk.execute(write_concern, session=session)
        except BulkWriteError as bwe:
            _raise_last_error(bwe.details)
        return ids