Unable to use MongoDB Change Streams. MongoError: The $changeStream stage is only supported on replica sets
I am trying to use Mongo Change Streams to listen for changes in a collection. Following is my code.
const collection = cmt.collection('foo');
const changeStream = collection.watch();
changeStream.on('change', async (next) => {
if(next && next.fullDocument){
let row = next.fullDocument;
console.log('event', row);
// dealing with row
}
});
But while registering, I am getting the following error
/Users/experimental/githome/foo/node_modules/mongodb/lib/utils.js:132
throw err;
^
MongoError: The $changeStream stage is only supported on replica sets
at Connection.<anonymous> (/Users/experimental/githome/foo/node_modules/mongodb-core/lib/connection/pool.js:443:61)
at Connection.emit (events.js:182:13)
at processMessage (/Users/experimental/githome/foo/node_modules/mongodb-core/lib/connection/connection.js:364:10)
at Socket.<anonymous> (/Users/experimental/githome/foo/node_modules/mongodb-core/lib/connection/connection.js:533:15)
at Socket.emit (events.js:182:13)
at addChunk (_stream_readable.js:283:12)
at readableAddChunk (_stream_readable.js:264:11)
at Socket.Readable.push (_stream_readable.js:219:10)
at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
Emitted 'error' event at:
at processNewChange (/Users/experimental/githome/foo/node_modules/mongodb/lib/change_stream.js:436:43)
at Cursor.<anonymous> (/Users/experimental/githome/foo/node_modules/mongodb/lib/change_stream.js:272:5)
at Cursor.emit (events.js:182:13)
at next (/Users/experimental/githome/foo/node_modules/mongodb/lib/cursor.js:1018:14)
at err (/Users/experimental/githome/foo/node_modules/mongodb/lib/utils.js:411:14)
at executeCallback (/Users/experimental/githome/foo/node_modules/mongodb/lib/utils.js:401:25)
at handleCallback (/Users/experimental/githome/foo/node_modules/mongodb/lib/utils.js:128:55)
at cursor._next (/Users/experimental/githome/foo/node_modules/mongodb/lib/operations/cursor_ops.js:194:21)
at queryCallback (/Users/experimental/githome/foo/node_modules/mongodb-core/lib/cursor.js:662:23)
at /Users/experimental/githome/foo/node_modules/mongodb-core/lib/connection/pool.js:397:18
at process._tickCallback (internal/process/next_tick.js:61:11)
What is the issue with my code/ setup?
1 Answer
MongoDB chain streams option is available in replica sets setup only, but not in a standalone installation. However, you can update your standalone installation to a single node replica set by following the below steps.
1. Locate the mongodb.conf
file and add the replica set details
Add the following replica set details to mongodb.conf
file
replication:
replSetName: "<replica-set name>"
Example:
replication:
replSetName: "rs0"
Note: Location in brew installed MongoDB /usr/local/etc/mongod.conf
2. Initiate the replica set using rs.initiate()
Login to MongoDB shell and run the command rs.initiate()
this will start your replica set. Logs looks like following on successful start
> rs.initiate()
{
"info2" : "no configuration specified. Using a default configuration for the set",
"me" : "127.0.0.1:27017",
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1577545731, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1577545731, 1)
}
Reference: https://onecompiler.com/posts/3vchuyxuh/enabling-replica-set-in-mongodb-with-just-one-node