Hi Python Programmers, In this article, you will learn everything about insert documents in MongoDB using Python with the help of examples. In the previous tutorial, we have seen all about how to create documents in MongoDB using Python.
Note:- Document in MongoDB represents the record in SQL database.
Headings of Contents
Python MongoDB Insert Document into Collection
Python PyMongo provides two functions to insert new documents into MongoDB collections.
If you don’t know how to create collections in MongoDB, please follow our previous tutorial because, in that tutorial, we have explained everything about creating a collection in MongoDB using Python.
- insert _one()
- insert_many()
insert_one():- insert_one() function is use to insert only one document into MongoDB collection.The first parameter of insert_one()
function is dictionary contains name and value of each field in the document you want to insert.
insert_many():-The insert_many()
method is used to insert multiple documents into the MongoDB Collections.
Example: Insert Document in MongoDB using Python
import pymongo
client = pymongo.MongoClient("mongodb://localhost:27017/")
# database
database = client["programmingfundadb"]
# collection
collection = database["employees"]
# document to be insert
record = {"vishvajit": "Vishvajit", "Occupation": "Software Developer", "age": 24}
# insert into collection
result = collection.insert_one(record)
print(result)
Output
<pymongo.results.InsertOneResult object at 0x00000290B0EBDD80>
Return Id Field
The insert_one()
function returns by default InsertOneResult object which has an inserted_id
property that holds the id of the record.
Example: Python MongoDB Insert Document
import pymongo
client = pymongo.MongoClient("mongodb://localhost:27017/")
# database
database = client["programmingfundadb"]
# collection
collection = database["employees"]
# document to be insert
record = {"name": "John", "Occupation": "Software Enginner", "age": 30}
# insert into collection
result = collection.insert_one(record)
print(result.inserted_id)
Output
616a66f217ca23e3953eac7b
Important Note:- If you do not specify the id field in your document, MongoDB will add an id field in your document automatically and assign a unique id of each document.
Insert Multiple Documents in MongoDB
To insert multiple documents into MongoDB collection, use insert_many()
method.The first parameter of the insert_many()
method is a list containing dictionaries you want to insert.
Example: Insert Document in MongoDB using Python
import pymongo
client = pymongo.MongoClient("mongodb://localhost:27017/")
# database
database = client["programmingfundadb"]
# collection
collection = database["employees"]
# list of documents to be insert
list_data = [
{"name": "John", "Occupation": "Software Enginner", "age": 30},
{"name": "Peter", "Occupation": "DBA", "age": 33},
{"name": "Jarsi", "Occupation": "Machine Learning Enginner", "age": 30},
{"name": "Harry", "Occupation": "Python Dev", "age": 35}
]
# insert into collection
result = collection.insert_many(list_data)
print(result.inserted_ids)
Output
[ObjectId('616a83fed28431f7a80bf993'), ObjectId('616a83fed28431f7a80bf994'), ObjectId('616a83fed28431f7a80bf995'), ObjectId('616a83fed28431f7a80bf996')]
Insert Documents with Specified Id
If you want to insert your own id field instead of MongoDB id, Then you can assign the _id field in the document you want to insert.
Example: Python MongoDB Insert Document
import pymongo
client = pymongo.MongoClient("mongodb://localhost:27017/")
# database
database = client["programmingfundadb"]
# collection
collection = database["students"]
# list of documents to be insert
list_data = [
{"_id":1, "name": "Pankaj", "Occupation": "tester", "age": 30},
{"_id":2, "name": "Jaismin", "Occupation": "Data Scientist", "age": 28},
{"_id":3, "name": "Palak", "Occupation": "Machine Learning Enginner", "age": 30},
{"_id":4, "name": "Alexndra", "Occupation": "Java Dev", "age": 35}
]
# insert into collection
result = collection.insert_many(list_data)
print(result.inserted_ids)
Output
[1, 2, 3, 4]
Note:- inserted_ids
property returns the list of the object’s id.
So in this way, you can specify your own id to all the documents.
Conclusion
So, in this article, you will learn everything about insert documents in MongoDB using Python with the help of examples.insert_one()
and insert_many()
these are the two best methods to insert new documents into MongoDB collections. I hope you don’t have any confusion regarding how to insert documents into MongoDB collections using Python programming.
If you find this post is helpful, please share, support, and keep visiting for further Python MongoDB tutorials.
Reference:- Click Here
Thanks for your valuable time ….