Wednesday, December 25, 2024
Google search engine
HomeLanguagesDifference Between insert(), insertOne(), and insertMany() in Pymongo

Difference Between insert(), insertOne(), and insertMany() in Pymongo

MongoDB is a NoSql Database that can be used to store data required by different applications. Python can be used to access MongoDB databases. Python requires a driver to access the databases. PyMongo enables interacting with MongoDB database from Python applications. The pymongo package acts as a native Python driver for MongoDB. Pymongo provides commands that can be used in Python applications to perform required action on the MongoDB. MongoDB offers three methods to insert records or documents into the database which are as follows: 

  • insert() : Used to insert a document or documents into a collection. If the collection does not exist, then insert() will create the collection and then insert the specified documents.

Syntax 
db.collection.insert(<document or array of documents>, 

writeConcern: <document>, 
ordered: <boolean> 

)
Parameter 

  • <document>: The document or record that is to be stored in the database
  • writeConcern: Optional.
  • ordered: Optional. Can be set to true or false. 

Example: 

Python3




# importing Mongoclient from pymongo
from pymongo import MongoClient 
   
   
myclient = MongoClient("mongodb://localhost:27017/")
   
# database 
db = myclient["GFG"]
   
# Created or Switched to collection 
# names: College
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"},
]
 
   
# Inserting the entire list in the collection
collection.insert(mylist)


Output:

python-mongodb-insert

  • insertOne() : Used to insert a single document or record into the database. If the collection does not exist, then insertOne() method creates the collection first and then inserts the specified document.

Syntax 
db.collection.insertOne(<document>, 

writeConcern: <document> 

)
Parameter  

  • <document> The document or record that is to be stored in the database
  • writeConcern: Optional.

Return Value: It returns the _id of the document inserted into the database.  

Note: The Pymongo command for insertOne() is insert_one() 
Example: 

Python3




# importing Mongoclient from pymongo
from pymongo import MongoClient
 
# Making Connection
myclient = MongoClient("mongodb://localhost:27017/")
 
# database
db = myclient["GFG"]
 
# Created or Switched to collection
# names: GeeksForGeeks
collection = db["Student"]
 
# Creating Dictionary of records to be
# inserted
record = { "_id": 5,
          "name": "Raju",
          "Roll No": "1005",
          "Branch": "CSE"}
 
 
 
# Inserting the record1 in the collection
# by using collection.insert_one()
rec_id1 = collection.insert_one(record)


Output: 

  • insertMany()

Syntax 
db.collection.insertMany([ <document 1>, <document 2>, … ], 

writeConcern: <document>, 
ordered: <boolean> 

)
Parameter  

  • <documents> The document or record that is to be stored in the database
  • writeConcern: Optional.
  • ordered: Optional. Can be set to true or false.

Return Value: It returns the _ids of the documents inserted into the database.  

Note: The Pymongo command for insertMany() is insert_many() 
Example:

Python3




# importing Mongoclient from pymongo
from pymongo import MongoClient 
   
   
myclient = MongoClient("mongodb://localhost:27017/")
   
# database 
db = myclient["GFG"]
   
# Created or Switched to collection 
# names: GeeksForGeeks
collection = db["College"]
 
mylist = [
  { "_id": 6, "name": "Deepanshu", "Roll No": "1006", "Branch":"CSE"},
  { "_id": 7, "name": "Anshul", "Roll No": "1007", "Branch":"IT"}
]
   
# Inserting the entire list in the collection
collection.insert_many(mylist)


Output:

python-mongodb-insert-many1.math-table { border-collapse: collapse; width: 100%; } .math-table td { border: 1px solid #5fb962; text-align: left !important; padding: 8px; } .math-table th { border: 1px solid #5fb962; padding: 8px; } .math-table tr>th{ background-color: #c6ebd9; vertical-align: middle; } .math-table tr:nth-child(odd) { background-color: #ffffff; }  

insert() insertOne() insertMany()
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
throws WriteResult.writeConcernError and WriteResult.writeError for write and non-write concern errors respectively throws either a writeError or writeConcernError exception. throws a BulkWriteError exception.
compatible with db.collection.explain() not compatible with db.collection.explain() not compatible with db.collection.explain()
If ordered is set to true and any document reports an error then the remaining documents are not inserted. If ordered is set to false then remaining documents are inserted even if an error occurs. If error is reported for the document it is not inserted into the database If ordered is set to true and any document reports an error then the remaining documents are not inserted. If ordered is set to false then remaining documents are inserted even if an error occurs.
returns an object that contains the status of the operation. returns the insert_id of the document inserted returns the insert_ids of the documents inserted
RELATED ARTICLES

Most Popular

Recent Comments