Open In App

Python MongoDB - bulk_write()

Last Updated : 04 Jul, 2025
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

bulk_write() method in PyMongo is used to perform multiple write operations (like insert, update, or delete) in a single batch. It improves performance by reducing the number of round-trips between the application and the database. This method is especially useful for handling large datasets or grouped operations efficiently.

Syntax

collection.bulk_write(requests, ordered=True)

Parameters :

  • requests: list of write operations like InsertOne(), UpdateOne(), DeleteOne(), etc.
  • ordered (optional): if True, operations run in order and stop on error. If False, continue on errors.

Example 1

In this example, bulk_write() is used to perform a series of operations on the "myTable" collection inserting two documents, deleting one and replacing another.

python
from pymongo import MongoClient, InsertOne, DeleteOne, ReplaceOne

client = MongoClient("mongodb://localhost:27017/")
database = client['database']
mycollection = database['myTable']

# Define bulk write operations
requests = [
    InsertOne({"Student name": "Cody"}),
    InsertOne({"Student name": "Drew"}),
    DeleteOne({"Student name": "Cody"}),
    ReplaceOne({"Student name": "Drew"}, {"Student name": "Andrew"}, upsert=True)
]

# Execute the bulk write operations
result = mycollection.bulk_write(requests)

# Print final documents in the collection
print("Documents after bulk_write():")
for doc in mycollection.find():
    print(doc)

Output

bulkwrite-eg1
Snapshot of Terminal showing Output of bulk_write()

Explanation:

  • Inserts two documents "Cody" and "Drew" and then deletes the one with "Cody".
  • Replaces the document with "Drew" by a new one with "Andrew" using ReplaceOne.

Example 2

In this Example, bulk_write() is used to perform multiple operations on the "myTable" collection inserting two documents, updating one by incrementing a value and deleting another.

python
from pymongo import MongoClient, InsertOne, DeleteOne, ReplaceOne, UpdateOne

client = MongoClient("mongodb://localhost:27017/")
database = client['database']
mycollection = database['myTable'] 

# defining the bulk write requests
requests = [
    InsertOne({"x": 5}),
    InsertOne({"y": 2}),
    UpdateOne({'x': 5}, {'$inc': {'x': 3}}),  # increments x from 5 to 8
    DeleteOne({"y": 2})  # deletes the document with y = 2
]

# executing the bulk operations
result = mycollection.bulk_write(requests)

# print final documents in the collection
print("Documents after bulk_write():")
for doc in mycollection.find():
    print(doc)

Output

bulkwrite-2
Snapshot of Terminal showing Output of bulk_write()

Explanation:

  • Inserts two documents one with "x: 5" and another with "y: 2".
  • Increments the value of "x" from 5 to 8 using UpdateOne.
  • Deletes the document where "y" is 2 using DeleteOne.

Related Articles:


Next Article
Article Tags :
Practice Tags :

Similar Reads