Aws documentdb 为Amazon DocumentDB启用更改流

Aws documentdb 为Amazon DocumentDB启用更改流,aws-documentdb,changestream,aws-documentdb-mongoapi,Aws Documentdb,Changestream,Aws Documentdb Mongoapi,我知道我们可以使用MongoShell为Amazon文档数据库启用(或禁用)更改流。是否可以从AWS控制台或MongoDB驱动程序启用更改流?AWS控制台:否。我认为这不是由DocumentDB群集参数控制的 MongoDB驱动程序:是的 DocumentDB Change Streams使用方法启用和禁用changestreams。 在中,使用的命令是adminCommand,它只是在admin数据库上调用runCommand。由于几乎所有驱动程序都支持在数据库上运行命令,所以您应该能够使用任

我知道我们可以使用MongoShell为Amazon文档数据库启用(或禁用)更改流。是否可以从AWS控制台或MongoDB驱动程序启用更改流?

AWS控制台:否。我认为这不是由DocumentDB群集参数控制的

MongoDB驱动程序:是的

DocumentDB Change Streams使用方法启用和禁用changestreams。 在中,使用的命令是
adminCommand
,它只是在
admin
数据库上调用
runCommand
。由于几乎所有驱动程序都支持在数据库上运行命令,所以您应该能够使用任何驱动程序启用/禁用更改流

以下代码使用pymongo启用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}

>>来自pymongo导入MongoClient
>>>client=MongoClient(“mongodb://:@xxxxxx.xxxxxx.us-east-1.docdb.amazonaws.com:27017/test\u db?ssl=true&ssl\u ca\u certs=rds组合ca bundle.pem&replicaSet=rs0&readPreference=secondary首选”)
>>>客户端['admin']。命令('aggregate',1,管道=[{'$listChangeStreams':1}],游标={})
{'waitedMS':0,'cursor':{'firstBatch':[],'id':0,'ns':'admin.$cmd'},'ok':1.0}
>>>客户端['admin']。命令('modifyChangeStreams',1,database='bar',collection='foo',enable=True)
{'ok':1.0}
>>>客户端['admin']。命令('aggregate',1,管道=[{'$listChangeStreams':1}],游标={})
{'waitedMS':0,'cursor':{'firstBatch':[{'database':'bar','collection':'foo'}],'id':0,'ns':'admin.$cmd'},'ok':1.0}

您可以使用modifyChangeStream API在Amazon DocumentDB集群上启用changestreams。使用mongo shell启用/禁用changestreams-

//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});
要从应用程序中启用更改流,可以使用@krg265的建议