Python MongoDB - bulk_write() Last Updated : 04 Jul, 2025 Comments Improve Suggest changes 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. Syntaxcollection.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) OutputSnapshot 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 2In 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) OutputSnapshot 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:insert_one()delete_one()update_one()replace_one() Comment More infoAdvertise with us G gauravbabbar25 Follow Improve Article Tags : Python Python-mongoDB Explore Python FundamentalsPython Introduction 3 min read Input and Output in Python 4 min read Python Variables 5 min read Python Operators 5 min read Python Keywords 2 min read Python Data Types 8 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 7 min read Python Functions 5 min read Recursion in Python 6 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 5 min read Python Tuples 4 min read Dictionaries in Python 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 6 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 2 min read Python MySQL 9 min read Python Packages 12 min read Python Modules 7 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 6 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 15+ min read StatsModel Library- Tutorial 4 min read Learning Model Building in Scikit-learn 8 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 7 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 7 min read Python | Build a REST API using Flask 3 min read How to Create a basic API using Django Rest Framework ? 4 min read Python PracticePython Quiz 3 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like