Python MongoDB – insert_many Query

MongoDB is a cross-platform document-oriented and a non relational (i.e NoSQL) database program. It is an open-source document database, that stores the data in the form of key-value pairs. MongoDB is developed by MongoDB Inc. and initially released on 11 February 2009. It is written in C++, Go, JavaScript, Python languages. MongoDB offers high speed, high availability, and high scalability.

insert_many()

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)

Parameters:



  • ‘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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing Mongoclient from pymongo
from pymongo import MongoClient 
  
  
myclient = MongoClient("mongodb://localhost:27017/")
  
# database 
db = myclient["GFG"]
  
# Created or Switched to collection 
# names: GeeksForGeeks
collection = db["Student"]
  
# Creating a list of records which we 
# insert in the collection using the
# update_many() method.
mylist = [
  { "_id": 1, "name": "Vishwash", "Roll No": "1001", "Branch":"CSE"},
  { "_id": 2, "name": "Vishesh", "Roll No": "1002", "Branch":"IT"},
  { "_id": 3, "name": "Shivam", "Roll No": "1003", "Branch":"ME"},
  { "_id": 4, "name": "Yash", "Roll No": "1004", "Branch":"ECE"},
]
  
# In the above list _id field is provided so it inserted in 
# the collection as specified.
  
# Inseting the entire list in the collection
collection.insert_many(mylist)

chevron_right


Output:

python-mongodb-insert-many

Example 2: In this example _id is not provided, it is allocated automatically by MongoDB.

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing Mongoclient from pymongo
from pymongo import MongoClient 
  
  
myclient = MongoClient("mongodb://localhost:27017/")
  
# database 
db = myclient["GFG"]
  
# Created or Switched to collection
# names: GeeksForGeeks
collection = db["Geeks"]
  
# Creating a list of records which we 
# insert in the collection using the
# update_many() method.
mylist = [
  {"Manufacturer":"Honda", "Model":"City", "Color":"Black"},
  {"Manufacturer":"Tata", "Model":"Altroz", "Color":"Golden"},
  {"Manufacturer":"Honda", "Model":"Civic", "Color":"Red"},
  {"Manufacturer":"Hyundai", "Model":"i20", "Color":"white"},
  {"Manufacturer":"Maruti", "Model":"Swift", "Color":"Blue"},
]
# In the above list we do not specify the _id, the MongoDB assigns 
# a unique id to all the records in the collection by default.
  
# Inseting the entire list in the collection
collection.insert_many(mylist)

chevron_right


Output :

python-mongodb-insert-many-2

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.




My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.