How to Insert a Record Or Document in Python MongoDB


In this example we will show how to insert a record or document using the insert_one() method in Python MongoDB.

Source Code

from pymongo import MongoClient

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

record = {"State_name": "Alabama", "Abbreviation": "AL", "Capital": "Montgomery", "Total_area": 52420}

a = collection.insert_one(record)
print(a)

Output:

<pymongo.results.InsertOneResult object at 0x02145238>
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments