Skip to content

DOCSP 13860: UpdateMany usage example #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jul 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions source/code-snippets/updateMany.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package main

import (
"context"
"fmt"

"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)

// Replace the uri string with your MongoDB deployment's connection string.
const uri = "mongodb+srv://<username>:<password>@<cluster-address>/test?w=majority"

func main() {
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri))

if err != nil {
panic(err)
}
defer func() {
if err = client.Disconnect(context.TODO()); err != nil {
panic(err)
}
}()

// begin updatemany
myCollection := client.Database("sample_airbnb").Collection("listingsAndReviews")
filter := bson.D{{"address.market", "Sydney"}}
update := bson.D{{"$mul", bson.D{{"price", 1.15}}}}

result, err := myCollection.UpdateMany(context.TODO(), filter, update)
// end updatemany

if err != nil {
panic(err)
}

fmt.Printf("Documents updated: %v\n", result.ModifiedCount)

}
2 changes: 1 addition & 1 deletion source/includes/run-example-tip.rst
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.. tip::

Read the :doc:`Usage Examples guide </usage-examples>` to learn how
to run this example.
to run this example.
53 changes: 53 additions & 0 deletions source/usage-examples/updateMany.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,56 @@ Update Multiple Documents
=========================

.. default-domain:: mongodb

You can update multiple documents in a collection by using the ``UpdateMany()``
method.

The following example passes a query filter and an update parameter to the
``UpdateMany()`` method, which matches documents in the
``listingsAndReviews`` collection where the value of the ``address.market``
field is "Sydney" and multiplies the ``price`` field in the matching
documents by a factor of *1.15*:

.. include:: /includes/run-example-tip.rst

.. literalinclude:: /code-snippets/updateMany.go
:start-after: begin updatemany
:end-before: end updatemany
:emphasize-lines: 5
:language: go
:dedent:

Click here **<TODO: github link to file>** to see a fully runnable example.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume similar to #5 (comment) this link, and the TODO link below will be updated before publishing.


Expected Result
---------------

After you run the code example, the ``price`` field of the matched
documents will reflect the multiplication. The updated documents will
appear similar to these:

.. code-block:: json
:copyable: false

// results truncated
...
{ "_id" : "10091713", ... , "name" : "Surry Hills Studio", ... , "price" : 181.00, ... },
{ "_id" : "9908871", ... , "name" : "Family friendly beach house", ... , "price" : 751.00, ... },
{ "_id" : "20989061", ... , "name" : "Big and sunny Narraben room", ... , "price" : 60.00, ... },
...

Additional Information
----------------------

For more information on updating documents, specifying query filters, and
handling potential errors, see our guide on **<TODO: change a document
fundamental page>**.

For more information on update operators,
see the :manual:`MongoDB update operator reference documentation
</reference/operator/update/#update-operators>`.

API Documentation
~~~~~~~~~~~~~~~~~

`UpdateMany() <https://pkg.go.dev/go.mongodb.org/[email protected]/mongo#Collection.UpdateMany>`_