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

19 Jan, 2022
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 pymongofrom pymongo import MongoClient       myclient = MongoClient("mongodb://localhost:27017/")   # database db = myclient["GFG"]   # Created or Switched to collection # names: Collegecollection = 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 collectioncollection.insert(mylist)
Output:
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 pymongofrom pymongo import MongoClient # Making Connectionmyclient = MongoClient("mongodb://localhost:27017/") # databasedb = myclient["GFG"] # Created or Switched to collection# names: GeeksForGeekscollection = db["Student"] # Creating Dictionary of records to be# insertedrecord = { "_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 pymongofrom pymongo import MongoClient       myclient = MongoClient("mongodb://localhost:27017/")   # database db = myclient["GFG"]   # Created or Switched to collection # names: GeeksForGeekscollection = 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 collectioncollection.insert_many(mylist)
Output:
.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; }  
clintra
Python-mongoDB
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.