29

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

 3 years ago
source link: https://www.geeksforgeeks.org/difference-between-insert-insertone-and-insertmany-in-pymongo/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
Difference Between insert(), insertOne(), and insertMany() in Pymongo
Related Articles
Difference Between insert(), insertOne(), and insertMany() in Pymongo
  • Last Updated : 26 May, 2020

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:

  1. 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.

    Return Value: A WriteResult object or BulkWriteResult object for single or bulk inserts respectively.

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # 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"}, 
    # Inseting the entire list in the collection 
    collection.insert(mylist) 

    Output:

    python-mongodb-insert

  2. 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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # 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: 

    python-mongodb-insert-one-1.png

  3. 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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # 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"}
    # Inseting the entire list in the collection 
    collection.insert_many(mylist) 

    Output:

    python-mongodb-insert-many1

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

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK