Skip to content

DOCSP-22867: describe ChangeStreamDocument #406

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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions source/fundamentals/crud/read-operations/change-streams.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,14 @@ changes in the connected MongoDB deployment.
Example
~~~~~~~

The following code example shows how to open a change stream and print
change stream events whenever the data in the collection changes:
This example shows how to open a change stream on the
``myColl`` collection and print change stream events as they occur.

The driver stores change stream events in a variable of type
``ChangeStreamIterable``. In the following example, we specify that the
driver should populate the ``ChangeStreamIterable`` object with ``Document``
types. As a result, the driver stores individual change stream events as
``ChangeStreamDocument`` objects.

.. literalinclude:: /includes/fundamentals/code-snippets/change-streams/ChangeStreams.java
:language: java
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[question]

This might be beyond the scope of this ticket, but when I was reading the example, I was wondering if it would be useful to show the definition of collection? Right now, we have this comment:

// The collection variable references a MongoCollection instance

I leave this up to your judgment, but wanted to share the thought.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The way the code is set up right now, a collection is passed as a parameter. I'm going to edit this to instantiate the collection inside the function though.

Expand All @@ -81,7 +87,7 @@ following text:
Received a change: ChangeStreamDocument{
operationType='insert',
resumeToken={"_data": "825EC..."},
namespace=myDb.myChangeStreamCollection,
namespace=myDb.myColl,
...
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ public static void main(String [] args){
// ChangeStreams.allFullDocumentOptions(collection);
// ChangeStreams.aggregationExample(collection);
}
private static void openExample(MongoCollection<Document> collection) {
private static void openExample(MongoDatabase database) {
// begin openChangeStreamExample
// The collection variable references a MongoCollection instance
ChangeStreamIterable<Document> changeStream = collection.watch();
MongoCollection<Document> collection = database.getCollection("myColl");

ChangeStreamIterable<Document> changeStream = collection.watch();
changeStream.forEach(event ->
System.out.println("Received a change: " + event));
// end openChangeStreamExample
Expand Down