Skip to content

Commit 4c6da3e

Browse files
DOCSP-13867 watch usage example (#27)
* added watch usage example page
1 parent 33d89e9 commit 4c6da3e

File tree

3 files changed

+102
-6
lines changed

3 files changed

+102
-6
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
var uri string
16+
if uri = os.Getenv("MONGODB_URI"); uri == "" {
17+
log.Fatal("You must set your 'MONGODB_URI' environmental variable. See\n\t https://docs.mongodb.com/drivers/go/current/usage-examples/")
18+
}
19+
20+
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri))
21+
if err != nil {
22+
panic(err)
23+
}
24+
defer func() {
25+
if err = client.Disconnect(context.TODO()); err != nil {
26+
panic(err)
27+
}
28+
}()
29+
30+
// begin watch
31+
coll := client.Database("insertDB").Collection("haikus")
32+
pipeline := mongo.Pipeline{bson.D{{"$match", bson.D{{"operationType", "insert"}}}}}
33+
cs, err := coll.Watch(context.TODO(), pipeline)
34+
if err != nil {
35+
panic(err)
36+
}
37+
defer cs.Close(context.TODO())
38+
39+
fmt.Println("Waiting For Change Events. Insert something in MongoDB!")
40+
41+
for cs.Next(context.TODO()) {
42+
var event bson.D
43+
if err := cs.Decode(&event); err != nil {
44+
panic(err)
45+
}
46+
fmt.Println(event[3].Value)
47+
}
48+
if err := cs.Err(); err != nil {
49+
panic(err)
50+
}
51+
// end watch
52+
}

source/usage-examples.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,10 @@ Usage Examples
2020
/usage-examples/struct-tagging
2121
/usage-examples/distinct
2222
/usage-examples/bulkWrite
23+
/usage-examples/watch
2324

2425
..
2526
/usage-examples/insert-operations
26-
/usage-examples/watch
27-
2827

2928
Overview
3029
--------

source/usage-examples/watch.txt

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,52 @@ Watch for Changes
44

55
.. default-domain:: mongodb
66

7-
You can keep track of changes to data in MongoDB, such as changes to a
8-
collection, database, or deployment, by opening a **change stream**. A change
9-
stream allows applications to watch for changes to data and react to them.
10-
The change stream returns **change event** documents when changes occur.
7+
You can open a change stream on a ``MongoCollection``,
8+
``MongoDatabase``, or ``MongoClient`` by using the ``Watch()`` method.
9+
10+
The following example uses the ``Watch()`` method, to open a change
11+
stream on the ``haikus`` collection and prints inserted documents:
12+
13+
.. include:: /includes/usage-examples/run-example-tip.rst
14+
15+
.. literalinclude:: /includes/usage-examples/code-snippets/watch.go
16+
:start-after: begin watch
17+
:end-before: end watch
18+
:emphasize-lines: 3
19+
:language: go
20+
:dedent:
21+
22+
Click `here <{+example+}/watch.go>`__ to see a fully runnable example.
23+
24+
Expected Result
25+
---------------
26+
27+
After you run the preceding example, run the :doc:`Insert a
28+
Document usage example </usage-examples/insertOne>` in a different
29+
shell. Once you run the insert operation, you should see output similar
30+
to the following:
31+
32+
.. code-block:: json
33+
:copyable: false
34+
35+
// results truncated
36+
[{_id ObjectID("...")} {title Record of ...} {text No bytes, no problem....}]
37+
38+
.. important::
39+
40+
Make sure to shut down this usage example once you finish by closing
41+
your terminal.
42+
43+
Additional Information
44+
----------------------
45+
46+
For more information on opening a change stream and handling
47+
potential errors, see the following guides:
48+
49+
- Go Driver <TODO: Change Stream Guide>
50+
- MongoDB Server Manual :manual:`Change Streams Documentation </changeStreams>`.
51+
52+
API Documentation
53+
~~~~~~~~~~~~~~~~~
54+
55+
`Watch() <{+godocs+}/mongo#Collection.Watch>`__

0 commit comments

Comments
 (0)