Skip to content

Commit 9dd2d63

Browse files
authored
Merge pull request #7 from rustagir/DOCSP-13860-updatemany-usage-ex
DOCSP 13860: UpdateMany usage example
2 parents 1291c10 + 6ebf50b commit 9dd2d63

File tree

3 files changed

+95
-1
lines changed

3 files changed

+95
-1
lines changed

source/code-snippets/updateMany.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"go.mongodb.org/mongo-driver/bson"
8+
"go.mongodb.org/mongo-driver/mongo"
9+
"go.mongodb.org/mongo-driver/mongo/options"
10+
)
11+
12+
// Replace the uri string with your MongoDB deployment's connection string.
13+
const uri = "mongodb+srv://<username>:<password>@<cluster-address>/test?w=majority"
14+
15+
func main() {
16+
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri))
17+
18+
if err != nil {
19+
panic(err)
20+
}
21+
defer func() {
22+
if err = client.Disconnect(context.TODO()); err != nil {
23+
panic(err)
24+
}
25+
}()
26+
27+
// begin updatemany
28+
myCollection := client.Database("sample_airbnb").Collection("listingsAndReviews")
29+
filter := bson.D{{"address.market", "Sydney"}}
30+
update := bson.D{{"$mul", bson.D{{"price", 1.15}}}}
31+
32+
result, err := myCollection.UpdateMany(context.TODO(), filter, update)
33+
// end updatemany
34+
35+
if err != nil {
36+
panic(err)
37+
}
38+
39+
fmt.Printf("Documents updated: %v\n", result.ModifiedCount)
40+
41+
}

source/includes/run-example-tip.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
.. tip::
22

33
Read the :doc:`Usage Examples guide </usage-examples>` to learn how
4-
to run this example.
4+
to run this example.

source/usage-examples/updateMany.txt

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,56 @@ Update Multiple Documents
33
=========================
44

55
.. default-domain:: mongodb
6+
7+
You can update multiple documents in a collection by using the ``UpdateMany()``
8+
method.
9+
10+
The following example passes a query filter and an update parameter to the
11+
``UpdateMany()`` method, which matches documents in the
12+
``listingsAndReviews`` collection where the value of the ``address.market``
13+
field is "Sydney" and multiplies the ``price`` field in the matching
14+
documents by a factor of *1.15*:
15+
16+
.. include:: /includes/run-example-tip.rst
17+
18+
.. literalinclude:: /code-snippets/updateMany.go
19+
:start-after: begin updatemany
20+
:end-before: end updatemany
21+
:emphasize-lines: 5
22+
:language: go
23+
:dedent:
24+
25+
Click here **<TODO: github link to file>** to see a fully runnable example.
26+
27+
Expected Result
28+
---------------
29+
30+
After you run the code example, the ``price`` field of the matched
31+
documents will reflect the multiplication. The updated documents will
32+
appear similar to these:
33+
34+
.. code-block:: json
35+
:copyable: false
36+
37+
// results truncated
38+
...
39+
{ "_id" : "10091713", ... , "name" : "Surry Hills Studio", ... , "price" : 181.00, ... },
40+
{ "_id" : "9908871", ... , "name" : "Family friendly beach house", ... , "price" : 751.00, ... },
41+
{ "_id" : "20989061", ... , "name" : "Big and sunny Narraben room", ... , "price" : 60.00, ... },
42+
...
43+
44+
Additional Information
45+
----------------------
46+
47+
For more information on updating documents, specifying query filters, and
48+
handling potential errors, see our guide on **<TODO: change a document
49+
fundamental page>**.
50+
51+
For more information on update operators,
52+
see the :manual:`MongoDB update operator reference documentation
53+
</reference/operator/update/#update-operators>`.
54+
55+
API Documentation
56+
~~~~~~~~~~~~~~~~~
57+
58+
`UpdateMany() <https://pkg.go.dev/go.mongodb.org/[email protected]/mongo#Collection.UpdateMany>`_

0 commit comments

Comments
 (0)