bulkWrite() – Run multiple write operation command

Runs multiple write operations with order of execution. Operations are run in order.

Syntax diagram

Read syntax diagramSkip visual syntax diagramdb.collection. bulkWrite({write operations})

Command parameters

write operations
Running of operations with the following content:
insertOne
  • Inserts a single document into the collection.
  • Syntax:
    -insertOne: <document>
    where document is a JSON document to be inserted.
updateOne
  • Updates a single document in a collection that matches a specified filter.
  • Syntax:
    - updateOne:<filter>,<update>,<upsert> 
    where
    • filter is the selection criteria for the update operation.
    • update is the update operation being run.
    • upsert is an optional boolean value that indicates whether to run an upsert operation. The default upsert value is false
updateMany
  • Updates all documents in a collection that matches a specified filter.
  • Syntax:
    - updateMany:<filter>,<update>,<upsert> 
    where
    • filter is the selection criteria for the update operation.
    • update is the update operation being run.
    • upsert is an optional boolean value that indicates whether to run an upsert operation. The default upsert value is false
replaceOne
  • Replaces a single document in a collection that matches a specified filter.
  • Syntax:
    - replaceOne:<filter>,<replace>,<upsert> 
    where
    • filter is the selection criteria for the replace operation.
    • replace is the document that is being replaced.
    • upsert is an optional boolean value that indicates whether to run an upsert operation. The default upsert value is false
deleteOne
  • Deletes all documents from a collection that matches a specified filter.
  • Syntax:
    - deleteOne: <query> 
    where query is a JSON query to filter a subset of data.
deleteMany
  • Deletes all documents from a collection that matches a specified filter.
  • Syntax:
    - deleteMany: <query>
    where query is a JSON query to filter a subset of data.

Output

A message is returned when the command is run successfully.

Examples

The following example shows the command syntax for inserting a document with the name value John and age value 20. The command also deletes a single document with the name value Jack and all the documents where the age value is greater than 22, in the collection mycollection:
db.mycollection.bulkWrite({
	insertOne : { name : “John”, age : 20 },
	deleteOne : { name : “Tim”},
	deleteMany : { “age” : { $gt : 22 } }
})
The following example shows the output returned by the previous example:
CDJSN1000I Command successfully completed.
The following example shows the command syntax for updating documents in the collection, mycollection. First, a document with the name value John is updated by setting the age value to 25. The command also updates multiple documents where the age value is less than 20, by changing the name value to Jack. The command also updates a document with a name value of Oliver by setting the age value to 18.:
db.mycollection.bulkWrite({
	updateOne : { 
			“filter” : { “name” : “John”},
			“update” : { $set : { “age” : 25 } }
		},
	updateMany : { 
			“filter” : { “age” : { $lt : 20 } },
			“update” : { $set : { “name” : “Jack” } }
		}, 
	replaceOne : { 
			“filter” : { “name” : “Oliver”},
			“replace” : { “name” : “Oliver”, “age” : 18 }
		}
})
The following example shows the successful output from the bulkWrite operation:
CDJSN1000I Command successfully completed.