Search code examples
mongoosemongo-collectionchangestreamaws-documentdbaws-documentdb-mongoapi

MongoError: Aggregation stage not supported: '$changeStream'


We have a sharded cluster of document db, hosted on AWS and using mongo db version 3.6 and we want to implement a collection.watch() function in the node.js code to get the changestream, whenever there is a change in the collection. While I am connection local host mongodb servers which are running in replica mode, it's working fine and I am getting the changestream in node js(client) but while I am trying to connect to the document db sharded cluster hosted on AWS, I am getting error MongoError: Aggregation stage not supported: '$changeStream'

I connected my local mongo db server running on localhost port(27001 and 27002) in replica mode, which is working fine but while connecting to AWS hosted document db cluster, it's giving an error as mentioned above.

here is the code which I am using to connect to document db cluster.

//setting mongoose connection using below code, which is working fine

    const mongoose = require('mongoose');
    let dev_db_url = 'mongodb://<userName>:<Password>@X.X.X.X:27001,X.X.X.X:27002/testDb?replicaSet=rs0&ssl=true';

    const mongoDB = process.env.MONGODB_URI || dev_db_url;
    mongoose.connect(mongoDB,{ useNewUrlParser: true });
    mongoose.Promise = global.Promise;
    const db = mongoose.connection;

After setting up connection, I am using below piece of code to implement watch function to look for the changeStream.

db.once('open', () => {
    console.log('::::::::::::::::::::Server Connected::::::::::::::::::::');
    const productCollection = db.collection('myCollectionName');
    const changeStream = productCollection.watch();
    changeStream.on('change', (change) => {
        console.log(change);      
    });
  });

Exact error I am getting in below line

 changeStream.on('change', (change) => {
        console.log(change);      
    });

Error is "MongoError: Aggregation stage not supported: '$changeStream'"

Exact Error Stack Trace:

    ::::::::::::::::::::Server Connected::::::::::::::::::::
    C:\Users\change_stream\node_modules\mongodb\lib\utils.js:132
          throw err;
          ^

    MongoError: Aggregation stage not supported: '$changeStream'
        at Connection.<anonymous> (C:\Users\change_stream\node_modules\mongodb-core\lib\connection\pool.js:443:61)
        at Connection.emit (events.js:198:13)
        at processMessage (C:\Users\change_stream\node_modules\mongodb-core\lib\connection\connection.js:364:10)
        at TLSSocket.<anonymous> (C:\Users\change_stream\node_modules\mongodb-core\lib\connection\connection.js:533:15)
        at TLSSocket.emit (events.js:198:13)
        at addChunk (_stream_readable.js:288:12)
        at readableAddChunk (_stream_readable.js:269:11)
        at TLSSocket.Readable.push (_stream_readable.js:224:10)
        at TLSWrap.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
    Emitted 'error' event at:
        at processNewChange (C:\Users\change_stream\node_modules\mongodb\lib\change_stream.js:436:43)
        at Cursor.<anonymous> (C:\Users\change_stream\node_modules\mongodb\lib\change_stream.js:272:5)
        at Cursor.emit (events.js:198:13)
        at next (C:\Users\change_stream\node_modules\mongodb\lib\cursor.js:1018:14)
        at err (C:\Users\change_stream\node_modules\mongodb\lib\utils.js:411:14)    at executeCallback (C:\Users\change_stream\node_modules\mongodb\lib\utils.js:401:25)
        at handleCallback (C:\Users\change_stream\node_modules\mongodb\lib\utils.js:128:55)
        at cursor._next (C:\Users\change_stream\node_modules\mongodb\lib\operations\cursor_ops.js:194:21)
        at queryCallback (C:\Users\change_stream\node_modules\mongodb-core\lib\cursor.js:662:23)
        at C:\Users\change_stream\node_modules\mongodb-core\lib\connection\pool.js:397:18
        at process._tickCallback (internal/process/next_tick.js:61:11)

My questions are here, Have anyone seen this kind of error? What should be the possible root cause of the error? Can I validate something on the mongodb cluster side, which can help in rectifying this error. I am expecting the changeStream to be printed in the console.log(change) but rather that printing its throwing this error.

Any help will be appreciated.

thanks!


Solution

  • It looks like DocumentDB does not support ChangeStreams.

    See the 'Query and Write Operations' section here: https://docs.aws.amazon.com/documentdb/latest/developerguide/mongo-apis-database.html