Enable change streams for Amazon DocumentDB

3.6k views Asked by At

I know we can use Mongo shell to enable(or disable) change streams for Amazon Document DB. Is it possible to enable the change streams from AWS Console or MongoDB Driver?

2

There are 2 answers

0
krg265 On

AWS Console: No. I don't think this is controlled by DocumentDB cluster params.

MongoDB Drivers: Yes

DocumentDB Change Streams uses runCommand method to enable and disable changestreams. In the documentation, the command used is adminCommand which is just calling runCommand on admin database. Since almost all drivers support running commands over database, you should be able to enable/disable change streams using any driver.

Following code uses pymongo to enable changestreams:

>>> from pymongo import MongoClient
>>> client = MongoClient("mongodb://<username>:<password>@xxxxxx.xxxxxx.us-east-1.docdb.amazonaws.com:27017/test_db?ssl=true&ssl_ca_certs=rds-combined-ca-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred")
>>> client['admin'].command('aggregate', 1, pipeline=[{'$listChangeStreams': 1}], cursor={})
{'waitedMS': 0, 'cursor': {'firstBatch': [], 'id': 0, 'ns': 'admin.$cmd'}, 'ok': 1.0}
>>> client['admin'].command('modifyChangeStreams', 1, database='bar', collection='foo', enable=True)
{'ok': 1.0}
>>> client['admin'].command('aggregate', 1, pipeline=[{'$listChangeStreams': 1}], cursor={})
{'waitedMS': 0, 'cursor': {'firstBatch': [{'database': 'bar', 'collection': 'foo'}], 'id': 0, 'ns': 'admin.$cmd'}, 'ok': 1.0}

0
meet-bhagdev On

You can enable changestreams on your Amazon DocumentDB cluster using the modifyChangeStream API. To enable/disable changestreams using the mongo shell -

//Enable change streams for the collection "foo" in database "bar"
db.adminCommand({modifyChangeStreams: 1,
    database: "bar",
    collection: "foo", 
    enable: true});
    

//Enable change streams for all collections in database "bar"
db.adminCommand({modifyChangeStreams: 1,
    database: "bar",
    collection: "", 
    enable: true});

//Enable change streams for all collections in all databases in a cluster
db.adminCommand({modifyChangeStreams: 1,
    database: "",
    collection: "", 
    enable: true});

To enable change streams from your application, you can use @krg265's suggestion.