Docs Menu
Docs Home
/ / /
Go Driver
/ / /

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

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

The examples in this guide use the following structs:

  • Book struct, which models documents in the db.books collection. Each document contains a description of a book that includes the title, author, and page length.

  • Poem struct, which models documents in the db.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.

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.

To define the write operations for your bulk operation on one namespace, create a WriteModel for each insert, replace, update, or delete.

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

SetDocument()

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}),
}

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

SetCollation()

The type of language collation to use when sorting results.

SetFilter()

The query filter specifying which document to replace.

SetHint()

The index to use to scan for documents.

SetReplacement()

The document to replace the matched document with.

SetSort()

The sort order for matching documents. The replace operation replaces only the first document according to the sort criteria.

SetUpsert()

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}),
}

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

SetArrayFilters()

The array elements the update applies to.

SetCollation()

The type of language collation to use when sorting results.

SetFilter()

The query filter specifying which document to update.

SetHint()

The index to use to scan for documents.

SetSort()

The criteria to use when ordering matching documents. This method is only available for the UpdateOneModel class.

SetUpdate()

The modifications to apply on the matched documents.

SetUpsert()

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}}}}),
}

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

SetCollation()

The type of language collation to use when sorting results.

SetFilter()

The query filter specifying which document to delete.

SetHint()

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}}}}),
}

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

SetBypassDocumentValidation()

Specifies whether the operation can opt-out of document level validation.
Default: false

SetComment()

Specifies a comment to attach to the operation.
Default: nil

SetLet()

Specifies a document with a list of values to improve operation readability. Values must be constant or closed expressions that don't reference document fields. For more information, see the let field for the delete and update commands in the MongoDB Server manual.
Default: nil

SetOrdered()

Specifies whether the driver stops performing write operations after an error occurs.
Default: true

The BulkWrite() method returns a BulkWriteResult type, which includes information about the bulk operation.

The BulkWriteResult type contains the following properties:

Property
Description

InsertedCount

The number of documents inserted.

MatchedCount

The number of documents matched by the query filter in update and replace operations.

ModifiedCount

The number of documents modified by update and replace operations.

DeletedCount

The number of documents deleted.

UpsertedCount

The number of documents upserted by update and replace operations.

UpsertedIDs

A map of an operation index to the _id of each upserted document.

Acknowledged

A boolean value that indicates whether the write operation was acknowledged.

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.

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)

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 document

  • Increments every document's length by 10 if the current length value is less than 200

  • 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}

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.

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>},
...
}

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

SetDocument()

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)},
}

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

SetCollation()

The type of language collation to use when sorting results.

SetFilter()

The query filter specifying which document to replace.

SetHint()

The index to use to scan for documents.

SetReplacement()

The document to replace the matched document with.

SetSort()

The sort order for matching documents. The replace operation replaces only the first document according to the sort criteria.

SetUpsert()

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 the title value is "Lucy"

  • Replace operation on the poems collection to replace a document in which the title 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})},
}

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

SetArrayFilters()

The array elements the update applies to.

SetCollation()

The type of language collation to use when sorting results.

SetFilter()

The query filter specifying which document to update.

SetHint()

The index to use to scan for documents.

SetSort()

The criteria to use when ordering matching documents. This method is only available for the ClientUpdateOneModel class.

SetUpdate()

The modifications to apply on the matched documents.

SetUpsert()

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 the author value is "Elena Ferrante"

  • Update operation on the poems collection to update a document in which the author 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.

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

SetCollation()

The type of language collation to use when sorting results.

SetFilter()

The query filter specifying which document to delete.

SetHint()

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 the length value is 103

  • Delete operation on the poems collection to delete a document in which the year value is 1855

writes := []mongo.ClientBulkWrite{
{"db", "books", mongo.NewClientDeleteOneModel().
SetFilter(bson.D{{"length", 103}})},
{"db", "poems", mongo.NewClientDeleteOneModel().
SetFilter(bson.D{{"year", 1855}})},
}

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

SetBypassDocumentValidation()

Specifies whether the operation can opt-out of document level validation.
Default: false

SetOrdered()

Specifies whether the driver stops performing write operations after an error occurs.
Default: true

SetComment()

Specifies a comment to attach to the operation.
Default: nil

SetLet()

Specifies a document with a list of values to improve operation readability. Values must be constant or closed expressions that don't reference document fields. For more information, see the let field for the delete and update commands in the MongoDB Server manual.
Default: nil

SetWriteConcern()

Specifies the write concern for the operations.
Default: nil

SetVerboseResults()

Specifies whether detailed information about each successful operation is included in the result.
Default: false

The BulkWrite() method returns a ClientBulkWriteResult type, which includes information about the bulk operation.

The ClientBulkWriteResult type contains the following properties:

Property
Description

InsertedCount

The number of documents inserted.

MatchedCount

The number of documents matched by the query filter in update and replace operations.

ModifiedCount

The number of documents modified by update and replace operations.

DeletedCount

The number of documents deleted.

UpsertedCount

The number of documents upserted by update and replace operations.

InsertResults

A map of an operation index to the _id value of each inserted document.

UpdateResults

A map of an operation index to the _id value of each updated document.

DeleteResults

A map of an operation index to the _id value of each deleted document.

Acknowledged

A boolean value that indicates whether the write operation was acknowledged.

HasVerboseResults

A boolean value that indicates whether the result contains detailed results.

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.

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)

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 and poems collections

  • Updates a document in the poems collection that has a title value of "The Raincoat"

  • Replaces a document in the books collection that has a title 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

For a runnable example on performing a bulk operation, see Perform Bulk Operations.

To learn more about performing the operations mentioned, see the following guides:

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:

Back

Upsert