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.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Difference Between insert(), insertOne(), and insertMany() in Pymongo
- Python MongoDB - Update_many Query
- Python MongoDB - insert_one Query
- Python MongoDB - find_one Query
- Python MongoDB - create_index Query
- Python MongoDB - find_one_and_delete Query
- Python MongoDB - Limit Query
- Python MongoDB - find_one_and_update Query
- Python MongoDB - drop_index Query
- Python MongoDB - Query
- Python MongoDB - find_one_and_replace Query
- MongoDB and Python
- MongoDB Python | Insert and Update Data
- MongoDB python | insert(), replace_one(), replace_many()
- MongoDB python | Delete Data and Drop Collection
- Python MongoDB - Find
- Python MongoDB - Sort
- Create a database in MongoDB using Python
- 3D Plotting sample Data from MongoDB Atlas Using Python
- Geospatial Queries with Python MongoDB
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.