Last Updated : 01 Jul, 2025
In PyMongo, inserting documents into a MongoDB collection can be done using different methods. Earlier versions used the insert() method, but it is now deprecated.
The recommended and modern approach is to use:
These methods provide better control, error handling and clarity.
insert() methodinsert() method was originally used in older versions of PyMongo to insert both single and multiple documents into a collection. It automatically handled either a single dictionary or a list of dictionaries.
Syntax:collection.insert(document)
collection.insert([document1, document2, ...])
Parameter: document representing dictionary or list of dictionaries.
Example: Python
from pymongo import MongoClient
myclient = MongoClient("mongodb://localhost:27017/")
db = myclient["GFG"]
collection = db["College"]
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"},
]
collection.insert(mylist)
Output
insert_one() methodinsert_one() method is used to insert a single document (i.e., one record) into a collection. Automatically adds a unique _id field if not provided.
Syntax:collection.insert_one(document)
Parameter: single dictionary representing the document to be inserted into the collection.
Example: Python
from pymongo import MongoClient
myclient = MongoClient("mongodb://localhost:27017/")
db = myclient["GFG"]
collection = db["Student"]
record = { "_id": 5,
"name": "Raju",
"Roll No": "1005",
"Branch": "CSE"}
rec_id1 = collection.insert_one(record)
Output
insert_many() methodinsert_many() method is used to insert multiple documents into a collection at once. Instead of calling insert_one() repeatedly for each document, insert_many() allows inserting a list of documents efficiently in a single operation.
Syntax:collection.insert_many([doc1, doc2, doc3,....])
Parameter: [doc1, doc2, doc3,....] represents a list of dictionary to be inserted into the collection.
Example: Python
from pymongo import MongoClient
myclient = MongoClient("mongodb://localhost:27017/")
db = myclient["GFG"]
collection = db["College"]
mylist = [
{ "_id": 6, "name": "Deepanshu", "Roll No": "1006", "Branch":"CSE"},
{ "_id": 7, "name": "Anshul", "Roll No": "1007", "Branch":"IT"}
]
collection.insert_many(mylist)
Output
Difference between insert(), insert_one() and insert_many() insert() insert_one() insert_many() Pymongo equivalent command is insert() Pymongo equivalent command is insert_one() Pymongo equivalent command is insert_many() Deprecated in newer versions of mongo engine Used in newer versions of mongo engine Used in newer versions of mongo engineSingle or multiple document can be inserted
Only one document can be inserted
Multiple documents (list of dictionary) can be inserted
Throws errors for write issues and write concern problems. Throws errors for write issues and write concern problems. Shows an error if there's a problem inserting multiple documents. Compatible with db.collection.explain() to understand how MongoDB processes it internally Not compatible with db.collection.explain() Not compatible with db.collection.explain() If ordered is True, MongoDB stops inserting when an error occurs.Related Article:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4