Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Difference Between insert(), insertOne(), and insertMany() in Pymongo
Next article icon

In MongoDB,insert_many() method is used to insert multiple documents into a collection at once. When working with Python, this operation is performed using the PyMongo library.

Instead of inserting documents one by one with insert_one(), insert_many() allows developers topass a list of dictionaries, where each dictionary represents a document. This is a more efficient and cleaner approach when inserting bulk data into MongoDB.

Syntax 

collection.insert_many(documents, ordered=True, bypass_document_validation=False, session=None, comment=None)

Parameters: 

Now, Let's explore some Examples:

Example 1:

In the below code, multiple student records are inserted withpredefined _id values into a collection.

Python
frompymongoimportMongoClientclient=MongoClient("mongodb://localhost:27017/")db=client["GFG"]collection=db["Student"]# List of student documents to insertstudents=[{"_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"}]# Inserting the documents into the collectioncollection.insert_many(students)

Output:

insert_manyex
Snapshot of Terminal showing Output of Insertion with _id provided

Example 2:

In this example_id is not provided, it is allocated automatically by MongoDB.  

Python
frompymongoimportMongoClient# Connect to MongoDBmyclient=MongoClient("mongodb://localhost:27017/")# Access the database and collectiondb=myclient["GFG"]collection=db["Geeks"]# List of documents to be insertedmylist=[{"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"},]# _id is not specified; MongoDB will assign unique ObjectId to each documentcollection.insert_many(mylist)

Output

2
Snapshot of Terminal showing ouput of Insertion without _id

Related Articles:


Article Tags :
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp