Skip to content

Commit ba1be7b

Browse files
DOCSP-13861 replace usage example (#23)
* added replace usage example page
1 parent 1741fc6 commit ba1be7b

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
16+
var uri string
17+
if uri = os.Getenv("MONGODB_URI"); uri == "" {
18+
log.Fatal("You must set your `MONGODB_URI' environmental variable. See\n\t https://docs.mongodb.com/drivers/go/current/usage-examples/")
19+
}
20+
21+
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri))
22+
if err != nil {
23+
panic(err)
24+
}
25+
defer func() {
26+
if err = client.Disconnect(context.TODO()); err != nil {
27+
panic(err)
28+
}
29+
}()
30+
31+
// begin replace
32+
coll := client.Database("insertDB").Collection("haikus")
33+
filter := bson.D{{"title", "Record of a Shriveled Datum"}}
34+
replacement := bson.D{{"title", "Dodging Greys"}, {"text", "When there're no matches, no longer need to panic. You can use upsert"}}
35+
36+
result, err := coll.ReplaceOne(context.TODO(), filter, replacement)
37+
// end replace
38+
39+
if err != nil {
40+
panic(err)
41+
}
42+
43+
// When you run this file for the first time, it should print: "Number of documents replaced: 1"
44+
if result.MatchedCount != 0 {
45+
fmt.Println("Number of documents replaced: %d\n", result.ModifiedCount)
46+
}
47+
}

source/usage-examples/replaceOne.txt

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

55
.. default-domain:: mongodb
6+
7+
You can replace a document in a collection by using the ``ReplaceOne()``
8+
method.
9+
10+
The following example specifies a query filter and replacement document
11+
to the ``ReplaceOne()`` method, which matches a document in the
12+
``haikus`` collection where the ``title`` field is "Record of a
13+
Shriveled Datum" and replaces it with a document that contains a
14+
``title`` and ``text`` field about a haiku:
15+
16+
.. include:: /includes/usage-examples/run-example-tip.rst
17+
18+
.. literalinclude:: /includes/usage-examples/code-snippets/replace.go
19+
:start-after: begin replace
20+
:end-before: end replace
21+
:emphasize-lines: 5
22+
:language: go
23+
:dedent:
24+
25+
Click `here <{+example+}/replace.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+
replacement document in the ``haikus`` collection:
32+
33+
.. code-block:: json
34+
:copyable: false
35+
36+
{
37+
"_id" : ObjectId("..."),
38+
"title" : "Dodging 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 replacing documents, specifying query filters, and
49+
handling potential errors, see our guide on **<TODO: change a document
50+
fundamental page>**.
51+
52+
API Documentation
53+
~~~~~~~~~~~~~~~~~
54+
55+
`ReplaceOne() <{+godocs+}/mongo#Collection.ReplaceOne>`__

0 commit comments

Comments
 (0)