This method is used to insert multiple entries in a collection or the database in MongoDB. The parameter of this method is a list that contains dictionaries of the data that we want to insert in the collection.
This method returns an instance of class “~pymongo.results.InsertManyResult” which has a “_id” field that holds the id of the inserted documents. If the document does not specify an “_id” field, then MongoDB will add the “_id” field to all the data in the list and assign a unique object id for the documents before inserting.
Syntax: collection.insert_many(documents, ordered=True, bypass_document_validation=False, session=None)
- ‘documents’ : A iterable of documents to insert.
- ‘ordered’ (optional): 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.
- ‘bypass_document_validation’ (optional) : If “True”, allows the write to opt-out of document level validation. Default is “False”.
- ‘session’ (optional): a class ‘~pymongo.client_session.ClientSession’.
Example 1: In this example _id is provided.
Example 2: In this example _id is not provided, it is allocated automatically by MongoDB.
- Python MongoDB - Query
- Python MongoDB - find_one Query
- Python MongoDB - find_one_and_delete Query
- Python MongoDB - insert_one Query
- Python MongoDB - find_one_and_update Query
- Python MongoDB - create_index Query
- Python MongoDB - Limit Query
- Python MongoDB - Update_many Query
- Python MongoDB - drop_index Query
- Difference Between insert(), insertOne(), and insertMany() in Pymongo
- MongoDB and Python
- Python MongoDB - Find
- Python MongoDB - Sort
- Python Mongodb - Delete_one()
- Python Mongodb - Delete_many()
- Python MongoDB - Update_one()
- Create a database in MongoDB using Python
- How to fetch data from MongoDB using Python?
- Geospatial Queries with Python MongoDB
- How to access a collection in MongoDB using Python?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.