Skip to content

Commit 1291c10

Browse files
authored
Merge pull request #5 from rustagir/DOCSP-13859-update-one-usage-ex
DOCSP-13859: UpdateOne usage example
2 parents ef1cb67 + 3ecc71b commit 1291c10

File tree

5 files changed

+100
-5
lines changed

5 files changed

+100
-5
lines changed

source/code-snippets/updateOne.go

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

source/includes/connect-guide-note.rst

Lines changed: 0 additions & 4 deletions
This file was deleted.

source/includes/run-example-tip.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.. tip::
2+
3+
Read the :doc:`Usage Examples guide </usage-examples>` to learn how
4+
to run this example.

source/usage-examples.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ Usage Examples
77
.. toctree::
88

99
/usage-examples/find-operations
10+
/usage-examples/update-operations
1011

1112
..
1213
/usage-examples/insert-operations
13-
/usage-examples/update-operations
1414
/usage-examples/delete-operations
1515
/usage-examples/bulkWrite
1616
/usage-examples/watch

source/usage-examples/updateOne.txt

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

55
.. default-domain:: mongodb
6+
7+
You can update a document in a collection by using the ``UpdateOne()``
8+
method.
9+
10+
The following example passes a query filter and an update parameter to the
11+
``UpdateOne()`` method, which matches a document in the ``restaurants`` collection
12+
by the value of its ``_id`` field and creates a new field in that document called
13+
``avg_rating`` with a value of *4.4*:
14+
15+
.. include:: /includes/run-example-tip.rst
16+
17+
.. literalinclude:: /code-snippets/updateOne.go
18+
:start-after: begin updateone
19+
:end-before: end updateone
20+
:emphasize-lines: 6
21+
:language: go
22+
:dedent:
23+
24+
Click here **<TODO: github link to file>** to see a fully runnable example.
25+
26+
Expected Result
27+
---------------
28+
29+
After you run the code example, the document will include the
30+
``avg_rating`` field with the specified value:
31+
32+
.. code-block:: json
33+
:copyable: false
34+
35+
// result truncated
36+
{
37+
"_id" : ObjectId("5eb3d668b31de5d588f42a7a"),
38+
...
39+
"name" : "Green House Cafe",
40+
"restaurant_id" : "40372112",
41+
"avg_rating" : 4.4
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+
`UpdateOne() <https://pkg.go.dev/go.mongodb.org/[email protected]/mongo#Collection.UpdateOne>`_

0 commit comments

Comments
 (0)