Skip to content

Commit a661697

Browse files
authored
DOCSP-43081: Change Streams (#37)
1 parent 46bb6d5 commit a661697

File tree

4 files changed

+387
-5
lines changed

4 files changed

+387
-5
lines changed

source/includes/read/change-streams.c

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#include <bson/bson.h>
2+
#include <mongoc/mongoc.h>
3+
#include <stdio.h>
4+
5+
int
6+
main (int argc, char *argv[])
7+
{
8+
mongoc_client_t *client;
9+
mongoc_collection_t *collection;
10+
mongoc_init ();
11+
12+
client =
13+
mongoc_client_new ("<connection string URI>");
14+
collection = mongoc_client_get_collection (client, "sample_restaurants", "restaurants");
15+
16+
{
17+
// Opens a change stream on the collection and prints each change
18+
// start-open-change-stream
19+
bson_t *pipeline = bson_new ();
20+
const bson_t *doc;
21+
22+
mongoc_change_stream_t *change_stream =
23+
mongoc_collection_watch (collection, pipeline, NULL);
24+
25+
while (true) {
26+
bson_error_t error;
27+
if (mongoc_change_stream_next (change_stream, &doc)) {
28+
char *str = bson_as_canonical_extended_json (doc, NULL);
29+
printf ("Received change: %s\n", str);
30+
bson_free (str);
31+
} else if (mongoc_change_stream_error_document (change_stream, &error, NULL)) {
32+
printf("Got error on change stream: %s\n", error.message);
33+
break;
34+
}
35+
}
36+
37+
bson_destroy (pipeline);
38+
mongoc_change_stream_destroy (change_stream);
39+
// end-open-change-stream
40+
}
41+
{
42+
// Updates a document in the collection to trigger a change event
43+
// start-update-for-change-stream
44+
bson_t *filter = BCON_NEW ("name", BCON_UTF8 ("Blarney Castle"));
45+
bson_t *update = BCON_NEW("$set", "{", "cuisine", BCON_UTF8 ("Irish"), "}");
46+
47+
mongoc_collection_update_one (collection, filter, update, NULL, NULL, NULL);
48+
// end-update-for-change-stream
49+
}
50+
{
51+
// Opens a change stream on the collection and specifies a pipeline to only receive update events
52+
// start-change-stream-pipeline
53+
bson_t *pipeline = BCON_NEW (
54+
"pipeline", "[",
55+
"{", "$match", "{", "operationType", BCON_UTF8 ("update"), "}", "}",
56+
"]");
57+
const bson_t *doc;
58+
59+
mongoc_change_stream_t *change_stream =
60+
mongoc_collection_watch (collection, pipeline, NULL);
61+
62+
while (mongoc_change_stream_next (change_stream, &doc)) {
63+
char *str = bson_as_canonical_extended_json (doc, NULL);
64+
printf ("Received change: %s\n", str);
65+
bson_free (str);
66+
}
67+
68+
bson_destroy (pipeline);
69+
mongoc_change_stream_destroy (change_stream);
70+
// end-change-stream-pipeline
71+
}
72+
{
73+
// Opens a change stream on the collection and specifies options to receive the full document for update events
74+
// start-change-stream-post-image
75+
bson_t *pipeline = bson_new ();
76+
bson_t *opts = BCON_NEW ("fullDocument", BCON_UTF8 ("updateLookup"));
77+
const bson_t *doc;
78+
79+
mongoc_change_stream_t *change_stream =
80+
mongoc_collection_watch (collection, pipeline, opts);
81+
82+
while (true) {
83+
bson_error_t error;
84+
if (mongoc_change_stream_next (change_stream, &doc)) {
85+
char *str = bson_as_canonical_extended_json (doc, NULL);
86+
printf ("Received change: %s\n", str);
87+
bson_free (str);
88+
} else if (mongoc_change_stream_error_document (change_stream, &error, NULL)) {
89+
printf("Got error on change stream: %s\n", error.message);
90+
break;
91+
}
92+
}
93+
94+
bson_destroy (pipeline);
95+
bson_destroy (opts);
96+
mongoc_change_stream_destroy (change_stream);
97+
// end-change-stream-post-image
98+
}
99+
100+
mongoc_collection_destroy (collection);
101+
mongoc_client_destroy (client);
102+
mongoc_cleanup ();
103+
104+
return EXIT_SUCCESS;
105+
}

source/read.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Read Data from MongoDB
2929
/read/count
3030
/read/distinct
3131
/read/cursors
32+
/read/change-streams
3233

3334
Overview
3435
--------
@@ -152,6 +153,5 @@ subsequent change events in that collection:
152153
:copyable:
153154
:dedent:
154155

155-
.. TODO: uncomment when watch change stream guide is complete
156-
.. To learn more about the ``mongoc_collection_watch()`` function, see the
157-
.. :ref:`c-change-streams` guide.
156+
To learn more about the ``mongoc_collection_watch()`` function, see the
157+
:ref:`c-change-streams` guide.

0 commit comments

Comments
 (0)