|
| 1 | +--- |
| 2 | +title: Change streams in Azure Cosmos DB’s API for MongoDB |
| 3 | +description: Learn how to use change streams n Azure Cosmos DB’s API for MongoDB to get the changes made to your data. |
| 4 | +author: srchi |
| 5 | +ms.service: cosmos-db |
| 6 | +ms.subservice: cosmosdb-mongo |
| 7 | +ms.topic: conceptual |
| 8 | +ms.date: 11/16/2019 |
| 9 | +ms.author: srchi |
| 10 | +--- |
| 11 | + |
| 12 | +# Change streams in Azure Cosmos DB’s API for MongoDB |
| 13 | + |
| 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. |
| 15 | + |
| 16 | +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. |
| 17 | + |
| 18 | +```javascript |
| 19 | +var cursor = db.coll.watch( |
| 20 | + [ |
| 21 | + { $match: { "operationType": { $in: ["insert", "update", "replace"] } } }, |
| 22 | + { $project: { "_id": 1, "fullDocument": 1, "ns": 1, "documentKey": 1 } } |
| 23 | + ], |
| 24 | + { fullDocument: "updateLookup" }); |
| 25 | + |
| 26 | +while (!cursor.isExhausted()) { |
| 27 | + if (cursor.hasNext()) { |
| 28 | + printjson(cursor.next()); |
| 29 | + } |
| 30 | +} |
| 31 | +``` |
| 32 | + |
| 33 | +The following example shows how to get changes to the items in a single shard, such as shard "a" that has items with value "1". |
| 34 | + |
| 35 | +```javascript |
| 36 | +var cursor = db.coll.watch( |
| 37 | + [ |
| 38 | + { |
| 39 | + $match: { |
| 40 | + $and: [ |
| 41 | + { "fullDocument.a": 1 }, |
| 42 | + { "operationType": { $in: ["insert", "update", "replace"] } } |
| 43 | + ] |
| 44 | + } |
| 45 | + }, |
| 46 | + { $project: { "_id": 1, "fullDocument": 1, "ns": 1, "documentKey": 1} } |
| 47 | + ], |
| 48 | + { fullDocument: "updateLookup" }); |
| 49 | + |
| 50 | +``` |
| 51 | + |
| 52 | +## Current limitations |
| 53 | + |
| 54 | +The following limitations are applicable when using change streams: |
| 55 | + |
| 56 | +* The `operationType` and `updateDescription` properties are not yet supported in the output document. |
| 57 | +* The `insert`, `update`, and `replace` operations types are currently supported. Delete operation or other events are not yet supported. |
| 58 | + |
| 59 | +Due to these limitations, the $match stage, $project stage, and fullDocument options are required as shown in the previous examples. |
| 60 | + |
| 61 | +## Error handling |
| 62 | + |
| 63 | +The following error codes and messages are supported when using change streams: |
| 64 | + |
| 65 | +* **HTTP error code 429** - When the change stream is throttled, it returns an empty page. |
| 66 | + |
| 67 | +* **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. |
| 68 | + |
| 69 | +## Next steps |
| 70 | + |
| 71 | +* [Use time to live to expire data automatically in Azure Cosmos DB's API for MongoDB](mongodb-time-to-live.md) |
| 72 | +* [Indexing in Azure Cosmos DB's API for MongoDB](mongodb-indexing.md) |
0 commit comments