Skip to content

Commit e61e6f0

Browse files
authored
Merge pull request #109676 from timsander1/master
update change streams doc
2 parents e00f914 + b8c87db commit e61e6f0

File tree

1 file changed

+58
-29
lines changed

1 file changed

+58
-29
lines changed

articles/cosmos-db/mongodb-change-streams.md

Lines changed: 58 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,45 @@
11
---
2-
title: Change streams in Azure Cosmos DBs API for MongoDB
3-
description: Learn how to use change streams n Azure Cosmos DBs API for MongoDB to get the changes made to your data.
4-
author: srchi
2+
title: Change streams in Azure Cosmos DB's API for MongoDB
3+
description: Learn how to use change streams in Azure Cosmos DB's API for MongoDB to get the changes made to your data.
4+
author: timsander1
55
ms.service: cosmos-db
66
ms.subservice: cosmosdb-mongo
77
ms.topic: conceptual
8-
ms.date: 11/16/2019
9-
ms.author: srchi
8+
ms.date: 03/30/2020
9+
ms.author: tisande
1010
---
1111

12-
# Change streams in Azure Cosmos DBs API for MongoDB
12+
# Change streams in Azure Cosmos DB's API for MongoDB
1313

14-
[Change feed](change-feed.md) support in Azure Cosmos DBs API for MongoDB is available by using the change streams API. By using the change streams API, your applications can get the changes made to the collection or to the items in a single shard. Later you can take further actions based on the results. Changes to the items in the collection are captured in the order of their modification time and the sort order is guaranteed per shard key.
14+
[Change feed](change-feed.md) support in Azure Cosmos DB's API for MongoDB is available by using the change streams API. By using the change streams API, your applications can get the changes made to the collection or to the items in a single shard. Later you can take further actions based on the results. Changes to the items in the collection are captured in the order of their modification time and the sort order is guaranteed per shard key.
1515

1616
> [!NOTE]
17-
> To use change streams, create the account with version 3.6 of Azure Cosmos DB's API for MongoDB, or a later version. If you run the change stream examples against an earlier version, you might see the `Unrecognized pipeline stage name: $changeStream` error.
17+
> To use change streams, create the account with version 3.6 of Azure Cosmos DB's API for MongoDB, or a later version. If you run the change stream examples against an earlier version, you might see the `Unrecognized pipeline stage name: $changeStream` error.
1818
19-
The following example shows how to get change streams on all the items in the collection. This example creates a cursor to watch items when they are inserted, updated, or replaced. The $match stage, $project stage, and fullDocument option are required to get the change streams. Watching for delete operations using change streams is currently not supported. As a workaround, you can add a soft marker on the items that are being deleted. For example, you can add an attribute in the item called "deleted" and set it to "true" and set a TTL on the item, so that you can automatically delete it as well as track it.
19+
## Current limitations
20+
21+
The following limitations are applicable when using change streams:
22+
23+
* The `operationType` and `updateDescription` properties are not yet supported in the output document.
24+
* The `insert`, `update`, and `replace` operations types are currently supported. Delete operation or other events are not yet supported.
25+
26+
Due to these limitations, the $match stage, $project stage, and fullDocument options are required as shown in the previous examples.
27+
28+
Unlike the change feed in Azure Cosmos DB's SQL API, there is not a separate [Change Feed Processor Library](change-feed-processor.md) to consume change streams or a need for a leases container. There is not currently support for [Azure Functions triggers](change-feed-functions.md) to process change streams.
29+
30+
## Error handling
31+
32+
The following error codes and messages are supported when using change streams:
33+
34+
* **HTTP error code 16500** - When the change stream is throttled, it returns an empty page.
35+
36+
* **NamespaceNotFound (OperationType Invalidate)** - If you run change stream on the collection that does not exist or if the collection is dropped, then a `NamespaceNotFound` error is returned. Because the `operationType` property can't be returned in the output document, instead of the `operationType Invalidate` error, the `NamespaceNotFound` error is returned.
37+
38+
## Examples
39+
40+
The following example shows how to get change streams on all the items in the collection. This example creates a cursor to watch items when they are inserted, updated, or replaced. The `$match` stage, `$project` stage, and `fullDocument` option are required to get the change streams. Watching for delete operations using change streams is currently not supported. As a workaround, you can add a soft marker on the items that are being deleted. For example, you can add an attribute in the item called "deleted." When you'd like to delete the item, you can set "deleted" to `true` and set a TTL on the item. Since updating "deleted" to `true` is an update, this change will be visible in the change stream.
41+
42+
### JavaScript:
2043

2144
```javascript
2245
var cursor = db.coll.watch(
@@ -33,13 +56,36 @@ while (!cursor.isExhausted()) {
3356
}
3457
```
3558

36-
The following example shows how to get changes to the items in a single shard. This example gets the changes of items that have shard key equal to "a" and the shard key value equal to "1".
59+
### C#:
60+
61+
```csharp
62+
var pipeline = new EmptyPipelineDefinition<ChangeStreamDocument<BsonDocument>>()
63+
.Match(change => change.OperationType == ChangeStreamOperationType.Insert || change.OperationType == ChangeStreamOperationType.Update || change.OperationType == ChangeStreamOperationType.Replace)
64+
.AppendStage<ChangeStreamDocument<BsonDocument>, ChangeStreamDocument<BsonDocument>, BsonDocument>(
65+
"{ $project: { '_id': 1, 'fullDocument': 1, 'ns': 1, 'documentKey': 1 }}");
66+
67+
var options = new ChangeStreamOptions{
68+
FullDocument = ChangeStreamFullDocumentOption.UpdateLookup
69+
};
70+
71+
var enumerator = coll.Watch(pipeline, options).ToEnumerable().GetEnumerator();
72+
73+
while (enumerator.MoveNext()){
74+
Console.WriteLine(enumerator.Current);
75+
}
76+
77+
enumerator.Dispose();
78+
```
79+
80+
## Changes within a single shard
81+
82+
The following example shows how to get changes to the items within a single shard. This example gets the changes of items that have shard key equal to "a" and the shard key value equal to "1". It is possible to have different clients reading changes from different shards in parallel.
3783

3884
```javascript
3985
var cursor = db.coll.watch(
4086
[
41-
{
42-
$match: {
87+
{
88+
$match: {
4389
$and: [
4490
{ "fullDocument.a": 1 },
4591
{ "operationType": { $in: ["insert", "update", "replace"] } }
@@ -52,23 +98,6 @@ var cursor = db.coll.watch(
5298

5399
```
54100

55-
## Current limitations
56-
57-
The following limitations are applicable when using change streams:
58-
59-
* The `operationType` and `updateDescription` properties are not yet supported in the output document.
60-
* The `insert`, `update`, and `replace` operations types are currently supported. Delete operation or other events are not yet supported.
61-
62-
Due to these limitations, the $match stage, $project stage, and fullDocument options are required as shown in the previous examples.
63-
64-
## Error handling
65-
66-
The following error codes and messages are supported when using change streams:
67-
68-
* **HTTP error code 429** - When the change stream is throttled, it returns an empty page.
69-
70-
* **NamespaceNotFound (OperationType Invalidate)** - If you run change stream on the collection that does not exist or if the collection is dropped, then a `NamespaceNotFound` error is returned. Because the `operationType` property can't be returned in the output document, instead of the `operationType Invalidate` error, the `NamespaceNotFound` error is returned.
71-
72101
## Next steps
73102

74103
* [Use time to live to expire data automatically in Azure Cosmos DB's API for MongoDB](mongodb-time-to-live.md)

0 commit comments

Comments
 (0)