How to Insert Multiple Documents Into a Collection in Python


In this example we will show how to insert multiple documents with the specified ids into a collection using the insert_many() method in Python MongoDB.

Source Code

from pymongo import MongoClient

client = MongoClient("localhost", 27017)
db = client["mydatabase"]
collection = db["us_state"]

records = [{"_id": 1, "State_name": "Alabama", "Abbreviation": "AL", "Capital": "Montgomery", "Total_area": 52420},
           {"_id": 2, "State_name": "Alaska", "Abbreviation": "AK", "Capital": "Juneau", "Total_area": 665384},
           {"_id": 3, "State_name": "Arizona", "Abbreviation": "AZ", "Capital": "Phoenix", "Total_area": 113990},
           {"_id": 4, "State_name": "Arkansas", "Abbreviation": "AR", "Capital": "Little Rock", "Total_area": 53179},
           {"_id": 5, "State_name": "California", "Abbreviation": "CA", "Capital": "Sacramento", "Total_area": 163695},
           {"_id": 6, "State_name": "Colorado", "Abbreviation": "CO", "Capital": "Denver", "Total_area": 104094},
           {"_id": 7, "State_name": "Connecticut", "Abbreviation": "CT", "Capital": "Hartford", "Total_area": 5543},
           {"_id": 8, "State_name": "Delaware", "Abbreviation": "DE", "Capital": "Dover", "Total_area": 2489},
           {"_id": 9, "State_name": "Florida", "Abbreviation": "FL", "Capital": "Tallahassee", "Total_area": 65758},
           {"_id": 10, "State_name": "Georgia", "Abbreviation": "GA", "Capital": "Atlanta", "Total_area": 59425}
           ]

a = collection.insert_many(records)
print(a.inserted_ids)

Output:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

How to Insert Multiple Documents Into a Collection in Python


In this example we will show how to insert multiple documents into a collection using the insert_many() method in Python MongoDB.

Source Code

from pymongo import MongoClient

client = MongoClient("localhost", 27017)
db = client["mydatabase"]
collection = db["us_state"]

records = [{"State_name": "Alaska", "Abbreviation": "AK", "Capital": "Juneau", "Total_area": 665384},
           {"State_name": "Arizona", "Abbreviation": "AZ", "Capital": "Phoenix", "Total_area": 113990},
           {"State_name": "Arkansas", "Abbreviation": "AR", "Capital": "Little Rock", "Total_area": 53179},
           {"State_name": "California", "Abbreviation": "CA", "Capital": "Sacramento", "Total_area": 163695},
           {"State_name": "Colorado", "Abbreviation": "CO", "Capital": "Denver", "Total_area": 104094},
           {"State_name": "Connecticut", "Abbreviation": "CT", "Capital": "Hartford", "Total_area": 5543},
           {"State_name": "Delaware", "Abbreviation": "DE", "Capital": "Dover", "Total_area": 2489},
           {"State_name": "Florida", "Abbreviation": "FL", "Capital": "Tallahassee", "Total_area": 65758},
           {"State_name": "Georgia", "Abbreviation": "GA", "Capital": "Atlanta", "Total_area": 59425},
           ]

a = collection.insert_many(records)
print(a.inserted_ids)

Output:

[ObjectId('5f22f311ec8fad73657688b3'), ObjectId('5f22f311ec8fad73657688b4'), ObjectId('5f22f311ec8fad73657688b5'), ObjectId('5f22f311ec8fad73657688b6'), ObjectId('5f22f311ec8fad73657688b7'), ObjectId('5f22f311ec8fad73657688b8'), ObjectId('5f22f311ec8fad73657688b9'), ObjectId('5f22f311ec8fad73657688ba'), ObjectId('5f22f311ec8fad73657688bb')]
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments