Insert an iterable of documents. >>> await db.test.count_documents({}) 0 >>> result = await db.test.insert_many([{'x': i} for i in range(2)]) >>> await result.inserted_ids [ObjectId('54f113fffba522406c9cc20e'), ObjectId('54f113fffba522406c9cc20f')]
(
self,
documents: Iterable[Union[_DocumentType, RawBSONDocument]],
ordered: bool = True,
bypass_document_validation: Optional[bool] = None,
session: Optional[AsyncClientSession] = None,
comment: Optional[Any] = None,
)
| 902 | |
| 903 | @_csot.apply |
| 904 | async def insert_many( |
| 905 | self, |
| 906 | documents: Iterable[Union[_DocumentType, RawBSONDocument]], |
| 907 | ordered: bool = True, |
| 908 | bypass_document_validation: Optional[bool] = None, |
| 909 | session: Optional[AsyncClientSession] = None, |
| 910 | comment: Optional[Any] = None, |
| 911 | ) -> InsertManyResult: |
| 912 | """Insert an iterable of documents. |
| 913 | |
| 914 | >>> await db.test.count_documents({}) |
| 915 | 0 |
| 916 | >>> result = await db.test.insert_many([{'x': i} for i in range(2)]) |
| 917 | >>> await result.inserted_ids |
| 918 | [ObjectId('54f113fffba522406c9cc20e'), ObjectId('54f113fffba522406c9cc20f')] |
| 919 | >>> await db.test.count_documents({}) |
| 920 | 2 |
| 921 | |
| 922 | :param documents: A iterable of documents to insert. |
| 923 | :param ordered: If ``True`` (the default) documents will be |
| 924 | inserted on the server serially, in the order provided. If an error |
| 925 | occurs all remaining inserts are aborted. If ``False``, documents |
| 926 | will be inserted on the server in arbitrary order, possibly in |
| 927 | parallel, and all document inserts will be attempted. |
| 928 | :param bypass_document_validation: (optional) If ``True``, allows the |
| 929 | write to opt-out of document level validation. Default is |
| 930 | ``False``. |
| 931 | :param session: a |
| 932 | :class:`~pymongo.asynchronous.client_session.AsyncClientSession`. |
| 933 | :param comment: A user-provided comment to attach to this |
| 934 | command. |
| 935 | |
| 936 | :return: An instance of :class:`~pymongo.results.InsertManyResult`. |
| 937 | |
| 938 | .. seealso:: `Writes and ids <https://www.mongodb.com/docs/languages/python/pymongo-driver/current/crud/insert/#overview>`_ |
| 939 | |
| 940 | .. note:: `bypass_document_validation` requires server version |
| 941 | **>= 3.2** |
| 942 | |
| 943 | .. versionchanged:: 4.1 |
| 944 | Added ``comment`` parameter. |
| 945 | |
| 946 | .. versionchanged:: 3.6 |
| 947 | Added ``session`` parameter. |
| 948 | |
| 949 | .. versionchanged:: 3.2 |
| 950 | Added bypass_document_validation support |
| 951 | |
| 952 | .. versionadded:: 3.0 |
| 953 | """ |
| 954 | if ( |
| 955 | not isinstance(documents, abc.Iterable) |
| 956 | or isinstance(documents, abc.Mapping) |
| 957 | or not documents |
| 958 | ): |
| 959 | raise TypeError("documents must be a non-empty list") |
| 960 | inserted_ids: list[ObjectId] = [] |
| 961 |
nothing calls this directly
no test coverage detected