Skip to content

Commit 33d89e9

Browse files
DOCSP-13868 bulk usage example (#26)
* added bulk usage example page
1 parent 6b3a0b5 commit 33d89e9

File tree

3 files changed

+104
-7
lines changed

3 files changed

+104
-7
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
"os"
8+
9+
"go.mongodb.org/mongo-driver/bson"
10+
"go.mongodb.org/mongo-driver/mongo"
11+
"go.mongodb.org/mongo-driver/mongo/options"
12+
)
13+
14+
func main() {
15+
var uri string
16+
if uri = os.Getenv("MONGODB_URI"); uri == "" {
17+
log.Fatal("You must set your 'MONGODB_URI' environmental variable. See\n\t https://docs.mongodb.com/drivers/go/current/usage-examples/")
18+
}
19+
20+
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri))
21+
if err != nil {
22+
panic(err)
23+
}
24+
defer func() {
25+
if err = client.Disconnect(context.TODO()); err != nil {
26+
panic(err)
27+
}
28+
}()
29+
30+
// begin bulk
31+
coll := client.Database("insertDB").Collection("haikus")
32+
models := []mongo.WriteModel{
33+
mongo.NewReplaceOneModel().SetFilter(bson.D{{"title", "Record of a Shriveled Datum"}}).
34+
SetReplacement(bson.D{{"title", "Dodging Greys"}, {"text", "When there're no matches, no longer need to panic. You can use upsert"}}),
35+
mongo.NewUpdateOneModel().SetFilter(bson.D{{"title", "Dodging Greys"}}).
36+
SetUpdate(bson.D{{"$set", bson.D{{"title", "Dodge The Greys"},}},}),
37+
}
38+
opts := options.BulkWrite().SetOrdered(true)
39+
40+
results, err := coll.BulkWrite(context.TODO(), models, opts)
41+
// end bulk
42+
43+
if err != nil {
44+
panic(err)
45+
}
46+
47+
// When you run this file for the first time, it should print:
48+
// Number of documents replaced or modified: 2
49+
fmt.Printf("Number of documents replaced or modified: %d", results.ModifiedCount)
50+
}

source/usage-examples.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ Usage Examples
1919
/usage-examples/command
2020
/usage-examples/struct-tagging
2121
/usage-examples/distinct
22+
/usage-examples/bulkWrite
2223

2324
..
2425
/usage-examples/insert-operations
25-
/usage-examples/bulkWrite
2626
/usage-examples/watch
2727

2828

source/usage-examples/bulkWrite.txt

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,57 @@ Perform Bulk Operations
44

55
.. default-domain:: mongodb
66

7+
You can perform bulk write operations on a collection using the
8+
``BulkWrite()`` method.
79

8-
The ``bulkWrite()`` method performs batch write operations against a
9-
*single* collection. This method reduces the number of network round trips from
10-
your application to the server which therefore increases the potential
11-
throughput and performance. Since you only receive the success status after
12-
all the operations return, we recommend you use this if that meets the
13-
requirements of your use case.
10+
The following example specifies a slice of ``WriteModels`` and an option
11+
to the ``BulkWrite()`` method, which performs the following in order:
12+
13+
- Match a document where the ``title`` is "Record of a Shriveled Datum" and replace it with a new document
14+
- Match a document where the ``title`` is "Dodging Greys" and update that field to "Dodge The Greys"
15+
16+
.. include:: /includes/usage-examples/run-example-tip.rst
17+
18+
.. literalinclude:: /includes/usage-examples/code-snippets/bulk.go
19+
:start-after: begin bulk
20+
:end-before: end bulk
21+
:emphasize-lines: 10
22+
:language: go
23+
:dedent:
24+
25+
Click `here <{+example+}/bulk.go>`__ to see a fully runnable example.
26+
27+
Expected Result
28+
---------------
29+
30+
After you run the preceding code snippet, you should be able to find the
31+
document in the ``haikus`` collection:
32+
33+
.. code-block:: json
34+
:copyable: false
35+
36+
{
37+
"_id" : ObjectId("..."),
38+
"title" : "Dodge The Greys",
39+
"text" : "When there're no matches, no longer need to panic. You can use upsert."
40+
}
41+
42+
For an example on how to find a document, see our :doc:`Find
43+
One Usage Example </usage-examples/findOne>`.
44+
45+
Additional Information
46+
----------------------
47+
48+
For more information on performing bulk write operations on a collection
49+
and handling potential errors, see our guide on <TODO: Bulk Write
50+
Fundamentals>.
51+
52+
For more information, see our :manual:`Bulk Write Operations
53+
</core/bulk-write-operations/>` page.
54+
55+
API Documentation
56+
~~~~~~~~~~~~~~~~~
57+
58+
- `BulkWrite() <{+godocs+}/mongo#Collection.BulkWrite>`__
59+
- `NewUpdateOneModel() <{+godocs+}/mongo#NewUpdateOneModel>`__
60+
- `NewReplaceOneModel() <{+godocs+}/mongo#NewReplaceOneModel>`__

0 commit comments

Comments
 (0)