Search code examples
node.jsmongodbmongoosechangestream

Mongo watch change stream suddenly stopped working


I'm using mongo watch() to subscribe to change stream events. I've noticed that the change stream events automatically stopped without throwing any specific error and become idle. Then have to restart the server to listen to the change stream again. I'm not able to find out the specific reason for this strange behavior. We are using Nodejs server. mongoose for db connection and watch. If any one of you faced the same issue please guide me on that. We have 1 primary node and 2 secondary node cluster and hosted in mongodb atlas.


Solution

  • The collection.watch(...) method has to be called on the collection on every server restart. Common mistake is to call it once upon the creation of the collection. However, the database does not maintain reference to the result of this call as it does for other calls such as the collection.createIndexes(...).