[DOCS-13033] Document stopping/cancelling a change stream (unwatch) Created: 17/Sep/19  Updated: 30/Oct/23  Resolved: 11/Feb/20

Status: Closed
Project: Documentation
Component/s: change stream, drivers, manual
Affects Version/s: None
Fix Version/s: Server_Docs_20231030

Type: Bug Priority: Major - P3
Reporter: Dan Dascalescu Assignee: Mohammad Chughtai (Inactive)
Resolution: Fixed Votes: 0
Labels: docs-investigating, node
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Participants:
Days since reply: 4 years, 1 day ago
Story Points: 2

 Description   

Description

I can't find any documentation on how to "unwatch" a collection/database/cluster.

https://mongodb.github.io/node-mongodb-native/3.3/api/Collection.html#watch

https://mongodb.github.io/node-mongodb-native/3.3/api/Db.html#watch

https://mongodb.github.io/node-mongodb-native/3.3/api/MongoClient.html#watch

https://docs.mongodb.com/manual/changeStreams/

https://docs.mongodb.com/manual/reference/method/db.collection.watch/

https://docs.mongodb.com/manual/reference/method/db.watch/

 

There is however a related ticket, CSHARP-2372, which mentions a "cancellation token". How does that translate to the Node driver? Does the change stream close or end when it gets garbage collected?

A/C

Each subtask complete showing how to unwatch/cancel a change stream



 Comments   
Comment by Mohammad Chughtai (Inactive) [ 11/Feb/20 ]

This is arguably not a duplicate since it involves creating a doc about change streams in the legacy node site, however, this work does not need to be done because a doc has been created for the new node site that will live on docs.mongodb.com. See: https://jira.mongodb.org/browse/DOCSP-7905

Comment by Mohammad Chughtai (Inactive) [ 11/Feb/20 ]

This is arguably not a duplicate since it involves creating a doc about change streams in the legacy node site, however, this work does not need to be done because a doc has been created for the new node site that will live on docs.mongodb.com. See: https://jira.mongodb.org/browse/DOCSP-7905

 

Comment by Mohammad Chughtai (Inactive) [ 03/Feb/20 ]

matt.broadstone@mongodb.com ravind.kumar

Hey, this is being covered in the new driver docs as per https://jira.mongodb.org/browse/DOCSP-7905?filter=-1

Currently, there is an open PR for this: https://github.com/mongodb/docs-node/pull/52 

Let me know if there are any questions. 

-Moe

Comment by Daniel Aprahamian (Inactive) [ 18/Sep/19 ]

dandv you're right, my bad. You should be using the 'change' event. 'data' is an event on cursors, but it will not work on the actual ChangeStream object.

Comment by Dan Dascalescu [ 18/Sep/19 ]

Thanks for pointing out close.

Re. changeStream.on('data') - I don't see the data event documented at https://mongodb.github.io/node-mongodb-native/3.3/api/ChangeStream.html under Events; did you mean .on('change'), or is data another event?

Comment by Daniel Aprahamian (Inactive) [ 17/Sep/19 ]

Specifically for Node, you can close a Change Stream, which will stop the streaming of events:

Example:

const changeStream = collection.watch();
 
// After this, events will start streaming in
changeStream.on('data', data => console.log(data));
 
// After 10 seconds, stop streaming
setTimeout(() => changeStream.close(), 10000);

Comment by Ravind Kumar (Inactive) [ 17/Sep/19 ]

daniel.aprahamian@mongodb.com any insight here? There are some server-side bits I'll have to chase down as well. I don't see anything in the spec wr.t. the c reference, but maybe that's a driver-specific implementation.

Generated at Thu Feb 08 08:06:46 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.