-
Notifications
You must be signed in to change notification settings - Fork 34
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
rustagir
merged 11 commits into
mongodb:master
from
rustagir:DOCSP-13860-updatemany-usage-ex
Jul 28, 2021
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
754e633
DOCSP-13860: created UpdateMany usage example
rustagir 8d0f252
toc change and small fixes
rustagir aa91cbf
refresh
rustagir 65a36a5
PR fixes 1
rustagir 1345ffd
delete spacing in results
rustagir bd6aa44
PR fixes 2
rustagir b469982
link and context fixes
rustagir 1f97802
wording fix for consistency
rustagir 00bf6a2
standardize error handling
rustagir 39ae078
moved uri declaration
rustagir 6ebf50b
Merge branch 'master' into DOCSP-13860-updatemany-usage-ex
rustagir File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
|
||
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>`_ |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.