Difference Between insert(), insertOne(), and insertMany() in Pymongo
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:
- insert_one(): to insert a single document
- insert_many(): to insert multiple documents
These methods provide better control, error handling and clarity.
insert() method
insert() 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:
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() method
insert_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:
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() method
insert_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:
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 engine |
Single 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. If ordered is False, it tries to insert all documents, even if some fail. | If a document has an error, it won't be inserted. | If ordered is True, insertion stops on error. If False, remaining documents are still inserted. |
Returns an object with the operation's status. | Returns the inserted document's ID. | Returns IDs of inserted documents |
Related Article: