Send a batch of write operations to the server. Requests are passed as a list of write operation instances ( :class:`~pymongo.operations.InsertOne`, :class:`~pymongo.operations.UpdateOne`, :class:`~pymongo.operations.UpdateMany`, :class:`~pymongo.operations.R
(
self,
requests: Sequence[_WriteOp[_DocumentType]],
ordered: bool = True,
bypass_document_validation: Optional[bool] = None,
session: Optional[ClientSession] = None,
comment: Optional[Any] = None,
let: Optional[Mapping[str, Any]] = None,
)
| 696 | |
| 697 | @_csot.apply |
| 698 | def bulk_write( |
| 699 | self, |
| 700 | requests: Sequence[_WriteOp[_DocumentType]], |
| 701 | ordered: bool = True, |
| 702 | bypass_document_validation: Optional[bool] = None, |
| 703 | session: Optional[ClientSession] = None, |
| 704 | comment: Optional[Any] = None, |
| 705 | let: Optional[Mapping[str, Any]] = None, |
| 706 | ) -> BulkWriteResult: |
| 707 | """Send a batch of write operations to the server. |
| 708 | |
| 709 | Requests are passed as a list of write operation instances ( |
| 710 | :class:`~pymongo.operations.InsertOne`, |
| 711 | :class:`~pymongo.operations.UpdateOne`, |
| 712 | :class:`~pymongo.operations.UpdateMany`, |
| 713 | :class:`~pymongo.operations.ReplaceOne`, |
| 714 | :class:`~pymongo.operations.DeleteOne`, or |
| 715 | :class:`~pymongo.operations.DeleteMany`). |
| 716 | |
| 717 | >>> for doc in db.test.find({}): |
| 718 | ... print(doc) |
| 719 | ... |
| 720 | {'x': 1, '_id': ObjectId('54f62e60fba5226811f634ef')} |
| 721 | {'x': 1, '_id': ObjectId('54f62e60fba5226811f634f0')} |
| 722 | >>> # DeleteMany, UpdateOne, and UpdateMany are also available. |
| 723 | ... |
| 724 | >>> from pymongo import InsertOne, DeleteOne, ReplaceOne |
| 725 | >>> requests = [InsertOne({'y': 1}), DeleteOne({'x': 1}), |
| 726 | ... ReplaceOne({'w': 1}, {'z': 1}, upsert=True)] |
| 727 | >>> result = db.test.bulk_write(requests) |
| 728 | >>> result.inserted_count |
| 729 | 1 |
| 730 | >>> result.deleted_count |
| 731 | 1 |
| 732 | >>> result.modified_count |
| 733 | 0 |
| 734 | >>> result.upserted_ids |
| 735 | {2: ObjectId('54f62ee28891e756a6e1abd5')} |
| 736 | >>> for doc in db.test.find({}): |
| 737 | ... print(doc) |
| 738 | ... |
| 739 | {'x': 1, '_id': ObjectId('54f62e60fba5226811f634f0')} |
| 740 | {'y': 1, '_id': ObjectId('54f62ee2fba5226811f634f1')} |
| 741 | {'z': 1, '_id': ObjectId('54f62ee28891e756a6e1abd5')} |
| 742 | |
| 743 | :param requests: A list of write operations (see examples above). |
| 744 | :param ordered: If ``True`` (the default) requests will be |
| 745 | performed on the server serially, in the order provided. If an error |
| 746 | occurs all remaining operations are aborted. If ``False`` requests |
| 747 | will be performed on the server in arbitrary order, possibly in |
| 748 | parallel, and all operations will be attempted. |
| 749 | :param bypass_document_validation: (optional) If ``True``, allows the |
| 750 | write to opt-out of document level validation. Default is |
| 751 | ``False``. |
| 752 | :param session: a |
| 753 | :class:`~pymongo.client_session.ClientSession`. |
| 754 | :param comment: A user-provided comment to attach to this |
| 755 | command. |