Bulk Operations
On this page
- Overview
- Sample Data
- Collection Bulk Write
- Define Collection Bulk Write Models
- Modify Collection-Level Behavior
- Collection-Level Return Value
- Collection-Level Execution Order
- Client Bulk Write
- Define Client Bulk Write Models
- Modify Client-Level Behavior
- Client-Level Return Value
- Client-Level Execution Order
- Additional Information
- Related Operations
- API Documentation
Overview
In this guide, you can learn how to use the MongoDB Go Driver to perform bulk operations. Bulk operations reduce the number of calls to the server by performing multiple write operations in a single method.
The Collection
and Client
classes both provide a BulkWrite()
method. You can use the Collection.BulkWrite()
method to perform
multiple write operations on a single collection. You can use the
Client.BulkWrite()
method to perform bulk writes across
multiple namespaces. In MongoDB, a namespace consists of a database name
and a collection name.
Important
Client Bulk Write Server Requirement
To perform bulk operations on a Client
instance,
ensure that your application connects to MongoDB Server
v8.0 or later
Sample Data
The examples in this guide use the following structs:
Book
struct, which models documents in thedb.books
collection. Each document contains a description of a book that includes the title, author, and page length.Poem
struct, which models documents in thedb.poems
collection. Each document contains a description of a poem that includes the title, author, and publication year.
type Book struct { Title string Author string Length int32 } type Poem struct { Title string Author string Year int32 }
To run the examples in this guide, load the sample data into the
books
and poems
collection by using the following snippet:
bookColl := client.Database("db").Collection("books") poemColl := client.Database("db").Collection("poems") books := []interface{}{ Book{Title: "My Brilliant Friend", Author: "Elena Ferrante", Length: 331}, Book{Title: "Lucy", Author: "Jamaica Kincaid", Length: 103}, } poems := []interface{}{ Poem{Title: "Song of Myself", Author: "Walt Whitman", Year: 1855}, Poem{Title: "The Raincoat", Author: "Ada Limon", Year: 2018}, } bookInsert, err := bookColl.InsertMany(context.TODO(), books) poemInsert, err := poemColl.InsertMany(context.TODO(), poems)
Tip
Nonexistent Databases and Collections
If the necessary database and collection don't exist when you perform a write operation, the server implicitly creates them.
Collection Bulk Write
To perform a bulk operation on a single namespace, call the BulkWrite()
method on a collection and pass an array of WriteModel
documents as a parameter.
Define Collection Bulk Write Models
To define the write operations for your bulk operation on one
namespace, create a WriteModel
for each insert, replace,
update, or delete.
InsertOneModel
To define an insert operation for a bulk write, create an
InsertOneModel
specifying the document you want to insert. To
insert multiple documents, create an InsertOneModel
for each
document you want to insert.
You can specify the behavior of the InsertOneModel
by
using the following method:
Method | Description |
---|---|
| The document to insert. |
The following example creates two InsertOneModel
instances to
insert two documents into the books
collection:
models := []mongo.WriteModel{ mongo.NewInsertOneModel().SetDocument(Book{Title: "Beloved", Author: "Toni Morrison", Length: 324}), mongo.NewInsertOneModel().SetDocument(Book{Title: "Outline", Author: "Rachel Cusk", Length: 258}), }
ReplaceOneModel
To define a replace operation for a bulk write, create
a ReplaceOneModel
specifying the document you want
replace and a replacement document. To replace multiple
documents, create a ReplaceOneModel
for each document
you want to replace.
You can specify the behavior of the ReplaceOneModel
by
using the following methods:
Method | Description |
---|---|
| The type of language collation to use when sorting results. |
| The query filter specifying which document to replace. |
| The index to use to scan for documents. |
| The document to replace the matched document with. |
| The sort order for matching documents. The replace operation
replaces only the first document according to the sort criteria. |
| Whether to insert a new document if the query filter doesn't match any documents. |
The following example creates a ReplaceOneModel
to replace a
document in the books
collection in which the title
value is "Lucy"
:
models := []mongo.WriteModel{ mongo.NewReplaceOneModel().SetFilter(bson.D{{"title", "Lucy"}}). SetReplacement(Book{Title: "On Beauty", Author: "Zadie Smith", Length: 473}), }
UpdateOneModel and UpdateManyModel
To define an update operation for a bulk write, create
an UpdateOneModel
specifying the document you want to update
and an update document. To update
multiple documents, use an UpdateManyModel
.
You can specify the behavior of each of the update models by using the following methods:
Method | Description |
---|---|
| The array elements the update applies to. |
| The type of language collation to use when sorting results. |
| The query filter specifying which document to update. |
| The index to use to scan for documents. |
| The criteria to use when ordering matching documents.
This method is only available for the UpdateOneModel
class. |
| The modifications to apply on the matched documents. |
| Whether to insert a new document if the query filter doesn't match any documents. |
The following example creates an UpdateOneModel
to update
a document in the books
collection, decrementing a
document's length
by 15
if the author
is "Elena Ferrante"
:
models := []mongo.WriteModel{ mongo.NewUpdateOneModel().SetFilter(bson.D{{"author", "Elena Ferrante"}}). SetUpdate(bson.D{{"$inc", bson.D{{"length", -15}}}}), }
DeleteOneModel and DeleteManyModel
To define a delete operation for a bulk write, create a
DeleteOneModel
specifying the document you want to delete.
To delete multiple documents, use the DeleteManyModel
.
You can specify the behavior of each of the delete models by using the following methods:
Method | Description |
---|---|
| The type of language collation to use when sorting results. |
| The query filter specifying which document to delete. |
| The index to use to scan for documents. |
The following example creates a DeleteManyModel
to delete
documents in the books
collection in which the length
is
greater than 300
:
models := []mongo.WriteModel{ mongo.NewDeleteManyModel().SetFilter(bson.D{{"length", bson.D{{"$gt", 300}}}}), }
Modify Collection-Level Behavior
To modify the behavior of your bulk write operation, pass a BulkWriteOptions
instance to the BulkWrite()
method.
The BulkWriteOptions
type allows you to configure options by using the
following methods:
Method | Description |
---|---|
| Specifies whether the operation can opt-out of document level validation. Default: false |
| Specifies a comment to attach to the operation. Default: nil |
| |
| Specifies whether the driver stops performing write operations after an error occurs. Default: true |
Collection-Level Return Value
The BulkWrite()
method returns a BulkWriteResult
type, which
includes information about the bulk operation.
The BulkWriteResult
type contains the following properties:
Property | Description |
---|---|
| The number of documents inserted. |
| The number of documents matched by the query filter in update and replace operations. |
| The number of documents modified by update and replace operations. |
| The number of documents deleted. |
| The number of documents upserted by update and replace operations. |
| A map of an operation index to the |
| A boolean value that indicates whether the write operation was acknowledged. |
Collection-Level Execution Order
To specify whether the bulk write performs the operations
in order, you can set the Ordered
option
to a boolean value. To set this option, specify the Ordered
field of a BulkWriteOptions
instance.
Ordered
By default, the BulkWrite()
method runs bulk operations in
order you added them and stops if an error occurs.
Tip
This is equivalent to passing a value of true
to the SetOrdered()
method, as shown in the following code:
opts := options.BulkWrite().SetOrdered(true)
Unordered
To run bulk write operations in any order and continue if an error
occurs, pass a value of false
to the SetOrdered()
method. The method
reports errors after the operation completes.
The following example performs the following actions in any order:
Inserts two documents
Replaces a document where the
title
is "My Brilliant Friend" with a new documentIncrements every document's
length
by10
if the currentlength
value is less than200
Deletes all documents where the
author
field value includes"Jam"
models := []mongo.WriteModel{ mongo.NewInsertOneModel().SetDocument(Book{Title: "Middlemarch", Author: "George Eliot", Length: 904}), mongo.NewInsertOneModel().SetDocument(Book{Title: "Pale Fire", Author: "Vladimir Nabokov", Length: 246}), mongo.NewReplaceOneModel().SetFilter(bson.D{{"title", "My Brilliant Friend"}}). SetReplacement(Book{Title: "Atonement", Author: "Ian McEwan", Length: 351}), mongo.NewUpdateManyModel().SetFilter(bson.D{{"length", bson.D{{"$lt", 200}}}}). SetUpdate(bson.D{{"$inc", bson.D{{"length", 10}}}}), mongo.NewDeleteManyModel().SetFilter(bson.D{{"author", bson.D{{"$regex", "Jam"}}}}), } // Specifies that the bulk write is unordered opts := options.BulkWrite().SetOrdered(false) // Runs the bulk write operation and prints a summary of the // data changes results, err := bookColl.BulkWrite(context.TODO(), models, opts) if err != nil { panic(err) } fmt.Printf("Number of documents inserted: %d\n", results.InsertedCount) fmt.Printf("Number of documents replaced or updated: %d\n", results.ModifiedCount) fmt.Printf("Number of documents deleted: %d\n", results.DeletedCount)
Number of documents inserted: 2 Number of documents replaced or updated: 2 Number of documents deleted: 1
The following documents are present in the books
collection after
the bulk operation:
{"title":"Atonement","author":"Ian McEwan","length":351} {"title":"Middlemarch","author":"George Eliot","length":904} {"title":"Pale Fire","author":"Vladimir Nabokov","length":246}
Client Bulk Write
To perform a bulk operation across multiple namespaces, call the
BulkWrite()
method on your client and pass an array of
ClientWriteModel documents as a parameter.
Define Client Bulk Write Models
To specify the write operations for your bulk operation on multiple
namespaces, create a ClientWriteModel
for each insert,
replace, update, or delete. Pass each write model to the
ClientBulkWrite
struct and specify the target database and collection,
as shown in the following code:
writes := []mongo.ClientBulkWrite{ {"<database name>", "<collection name>", <write model>}, ... }
ClientInsertOneModel
To define an insert operation for a bulk write, create a ClientInsertOneModel
specifying the document you want to insert. To insert multiple documents, create
a ClientInsertOneModel
for each document you want to insert.
You can specify the behavior of the ClientInsertOneModel
by using the following method:
Method | Description |
---|---|
| The document to insert. |
The following example creates two ClientInsertOneModel
instances to
insert one document into the books
collection and one document into the
poems
collection:
bookInsertDoc := Book{Title: "Parable of the Sower", Author: "Octavia E. Butler", Length: 320} poemInsertDoc := Poem{Title: "Fame is a fickle food", Author: "Emily Dickinson", Year: 1659} writes := []mongo.ClientBulkWrite{ {"db", "books", mongo.NewClientInsertOneModel().SetDocument(bookInsertDoc)}, {"db", "poems", mongo.NewClientInsertOneModel().SetDocument(poemInsertDoc)}, }
ClientReplaceOneModel
To define a replace operation for a bulk write, create
a ClientReplaceOneModel
specifying the document
you want to replace and a replacement document.
To replace multiple documents, create a ClientReplaceOneModel
for
each document you want to replace.
You can specify the behavior of the ClientReplaceOneModel
by
using the following methods:
Method | Description |
---|---|
| The type of language collation to use when sorting results. |
| The query filter specifying which document to replace. |
| The index to use to scan for documents. |
| The document to replace the matched document with. |
| The sort order for matching documents. The replace operation
replaces only the first document according to the sort criteria. |
| Whether to insert a new document if the query filter doesn't match any documents. |
This example creates ClientReplaceOneModel
instances to define
the following operations:
Replace operation on the
books
collection to replace a document in which thetitle
value is"Lucy"
Replace operation on the
poems
collection to replace a document in which thetitle
value is"Song of Myself"
writes := []mongo.ClientBulkWrite{ {"db", "books", mongo.NewClientReplaceOneModel(). SetFilter(bson.D{{"title", "Lucy"}}). SetReplacement(Book{Title: "On Beauty", Author: "Zadie Smith", Length: 473})}, {"db", "poems", mongo.NewClientReplaceOneModel(). SetFilter(bson.D{{"title", "Song of Myself"}}). SetReplacement(Poem{Title: "America", Author: "Walt Whitman", Year: 1888})}, }
ClientUpdateOneModel and ClientUpdateManyModel
To define an update operation for a bulk write, create
a ClientUpdateOneModel
specifying the document you
want to update and an update document.
To update multiple documents, use a ClientUpdateManyModel
.
You can specify the behavior of each of the update models by using the following methods:
Method | Description |
---|---|
| The array elements the update applies to. |
| The type of language collation to use when sorting results. |
| The query filter specifying which document to update. |
| The index to use to scan for documents. |
| The criteria to use when ordering matching documents.
This method is only available for the ClientUpdateOneModel class. |
| The modifications to apply on the matched documents. |
| Whether to insert a new document if the query filter
doesn't match any documents. |
This example creates ClientUpdateOneModel
instances to define
the following operations:
Update operation on the
books
collection to update a document in which theauthor
value is"Elena Ferrante"
Update operation on the
poems
collection to update a document in which theauthor
value is"Ada Limon"
writes := []mongo.ClientBulkWrite{ {"db", "books", mongo.NewClientUpdateOneModel(). SetFilter(bson.D{{"author", "Elena Ferrante"}}). SetUpdate(bson.D{{"$inc", bson.D{{"length", -15}}}})}, {"db", "poems", mongo.NewClientUpdateOneModel(). SetFilter(bson.D{{"author", "Ada Limon"}}). SetUpdate(bson.D{{"author", "Ada Limón"}})}, }
Note
To update all documents that match the author
field
query filters in the preceding example, use ClientUpdateManyModel
instances.
ClientDeleteOneModel and ClientDeleteManyModel
To define a delete operation for a bulk write, create
a ClientDeleteOneModel
specifying the document you want
to delete. To delete multiple documents, use the
ClientDeleteManyModel
.
You can specify the behavior of each of the delete models by using the following methods:
Method | Description |
---|---|
| The type of language collation to use when sorting results. |
| The query filter specifying which document to delete. |
| The index to use to scan for documents. |
This example creates ClientDeleteOneModel
instances to define
the following operations:
Delete operation on the
books
collection to delete a document in which thelength
value is103
Delete operation on the
poems
collection to delete a document in which theyear
value is1855
writes := []mongo.ClientBulkWrite{ {"db", "books", mongo.NewClientDeleteOneModel(). SetFilter(bson.D{{"length", 103}})}, {"db", "poems", mongo.NewClientDeleteOneModel(). SetFilter(bson.D{{"year", 1855}})}, }
Modify Client-Level Behavior
To modify the behavior of your bulk write operation, pass a ClientBulkWriteOptions
instance to the BulkWrite()
method.
The ClientBulkWriteOptions
type allows you to configure options by using the
following methods:
Method | Description |
---|---|
| Specifies whether the operation can opt-out of document level validation. Default: false |
| Specifies whether the driver stops performing write operations after an error occurs. Default: true |
| Specifies a comment to attach to the operation. Default: nil |
| |
| Specifies the write concern for the operations. Default: nil |
| Specifies whether detailed information about each successful operation is
included in the result. Default: false |
Client-Level Return Value
The BulkWrite()
method returns a ClientBulkWriteResult
type, which
includes information about the bulk operation.
The ClientBulkWriteResult
type contains the following properties:
Property | Description |
---|---|
| The number of documents inserted. |
| The number of documents matched by the query filter in update and replace operations. |
| The number of documents modified by update and replace operations. |
| The number of documents deleted. |
| The number of documents upserted by update and replace operations. |
| A map of an operation index to the |
| A map of an operation index to the |
| A map of an operation index to the |
| A boolean value that indicates whether the write operation was acknowledged. |
| A boolean value that indicates whether the result contains detailed results. |
Client-Level Execution Order
To specify whether the bulk write performs the operations
in order, you can set the Ordered
option
to a boolean value. To set this option, specify the Ordered
field of a ClientBulkWriteOptions
instance.
Ordered
By default, the BulkWrite()
method executes bulk operations in
order you added them and stops if an error occurs.
Tip
This is equivalent to passing a value of true
to the SetOrdered()
method, as shown in the following code:
opts := options.ClientBulkWrite().SetOrdered(true)
Unordered
To run bulk write operations in any order and continue if an error
occurs, pass a value of false
to the SetOrdered()
method. The method
reports errors after the operation completes.
The following example performs the following actions in any order:
Inserts a new document into the
books
andpoems
collectionsUpdates a document in the
poems
collection that has atitle
value of"The Raincoat"
Replaces a document in the
books
collection that has atitle
value of"My Brilliant Friend"
writes := []mongo.ClientBulkWrite{ {"db", "books", mongo.NewClientInsertOneModel(). SetDocument(Book{Title: "Middlemarch", Author: "George Eliot", Length: 904})}, {"db", "poems", mongo.NewClientInsertOneModel(). SetDocument(Poem{Title: "Mad Girl's Love Song", Author: "Sylvia Plath", Year: 1953})}, {"db", "poems", mongo.NewClientUpdateOneModel(). SetFilter(bson.D{{"title", "The Raincoat"}}). SetUpdate(bson.D{{"title", "The Conditional"}})}, {"db", "books", mongo.NewClientReplaceOneModel(). SetFilter(bson.D{{"title", "My Brilliant Friend"}}). SetReplacement(Book{Title: "The Story of a New Name", Author: "Elena Ferrante", Length: 480})}, } opts := options.ClientBulkWrite().SetOrdered(false) results, err := client.BulkWrite(context.TODO(), writes, opts) if err != nil { panic(err) } fmt.Printf("Number of documents inserted: %d\n", results.InsertedCount) fmt.Printf("Number of documents replaced or updated: %d\n", results.ModifiedCount)
Number of documents inserted: 2 Number of documents replaced or updated: 2
Additional Information
For a runnable example on performing a bulk operation, see Perform Bulk Operations.
Related Operations
To learn more about performing the operations mentioned, see the following guides:
API Documentation
To learn more about the methods or types used for collection bulk writes, see the following API documentation:
To learn more about the methods or types used for client bulk writes, see the following API documentation: