On this page本页内容
Mongo.watch( pipeline, options )
For replica sets and sharded clusters only仅适用于副本集和分片群集
New in version 4.0.在版本4.0中新增。 Requires 需要将featureCompatibilityVersion
(fCV) set to "4.0"
or greater. featureCompatibilityVersion
(fCV)设置为"4.0"
或更高。For more information on fCV, see 有关fCV的更多信息,请参阅setFeatureCompatibilityVersion
.setFeatureCompatibilityVersion
。
Opens a change stream cursor for a replica set or a sharded cluster to report on all its non-打开副本集或分片群集的更改流游标,以报告其数据库中的所有非系统集合,但system
collections across its databases, with the exception of the admin
, local
, and the config
databases.admin
、local
和config
数据库除外。
pipeline | array |
|
options | document | Mongo.watch() . Mongo.watch() 行为的其他选项。 |
The options
document can contain the following fields and values:options
文档可以包含以下字段和值:
resumeAfter | document |
|
startAfter | document |
|
fullDocument | string |
|
batchSize | int |
|
maxAwaitTimeMS | int |
|
collation | document |
|
startAtOperationTime | Timestamp |
|
Mongo.watch()
is available for replica sets and sharded clusters:可用于副本集和分片群集:
Mongo.watch()
on any data-bearing member.Mongowatch()
。Mongo.watch()
on a mongos
instance.mongos
实例上发出Mongo.watch()
。You can only use 您只能将Mongo.watch()
with the Wired Tiger storage engine.Mongo.watch()
与Wired Tiger存储引擎配合使用。
majority
Supportmajority
支持Starting in MongoDB 4.2, change streams are available regardless of the 从MongoDB 4.2开始,无论"majority"
read concern support; that is, read concern majority
support can be either enabled (default) or disabled to use change streams."majority"
读关注点支持如何,都可以使用变更流;也就是说,可以启用(默认)或禁用读关注majority
支持来使用更改流。
In MongoDB 4.0 and earlier, change streams are available only if 在MongoDB 4.0及更早版本中,只有启用了"majority"
read concern support is enabled (default)."majority"
读关注点支持(默认),更改流才可用。
Mongo.watch()
Unlike the MongoDB Drivers, 与MongoDB驱动程序不同,mongosh
does not automatically attempt to resume a change stream cursor after an error. mongosh
不会在出错后自动尝试恢复更改流游标。The MongoDB drivers make one attempt to automatically resume a change stream cursor after certain errors.MongoDB驱动程序尝试在出现某些错误后自动恢复更改流游标。
Mongo.watch()
uses information stored in the oplog to produce the change event description and generate a resume token associated to that operation. 使用oplog中存储的信息生成更改事件描述,并生成与该操作关联的恢复令牌。If the operation identified by the resume token passed to the 如果传递给resumeAfter
or startAfter
option has already dropped off the oplog, Mongo.watch()
cannot resume the change stream.resumeAfter
或startAfter
选项的恢复令牌标识的操作已从oplog中删除,则Mongo.watch()
无法恢复更改流。
See Resume a Change Stream for more information on resuming a change stream.有关恢复更改流的详细信息,请参阅恢复更改流。
resumeAfter
to resume a change stream after an invalidate event (for example, a collection drop or rename) closes the stream. resumeAfter
恢复更改流。startAfter
在失效事件后启动新的更改流。The resume token 恢复令牌_data
type depends on the MongoDB versions and, in some cases, the feature compatibility version (fcv) at the time of the change stream's opening/resumption (i.e. a change in fcv value does not affect the resume tokens for already opened change streams):_data
类型取决于MongoDB版本,在某些情况下,还取决于更改流打开/恢复时的功能兼容性版本(fcv)(即fcv值的更改不会影响已打开的更改流的恢复令牌):
MongoDB Version | _data Type_data 类型 | |
---|---|---|
MongoDB 4.2 and later | "4.2" or "4.0" | Hex-encoded string (v1 ) |
MongoDB 4.0.7 and later | "4.0" or "3.6" | Hex-encoded string (v1 ) |
MongoDB 4.0.6 and earlier | "4.0" | Hex-encoded string (v0 ) |
MongoDB 4.0.6 and earlier | "3.6" | BinData |
MongoDB 3.6 | "3.6" | BinData |
With hex-encoded string resume tokens, you can compare and sort the resume tokens.使用十六进制编码的字符串恢复标记,可以对恢复标记进行比较和排序。
Regardless of the fcv value, a 4.0 deployment can use either BinData resume tokens or hex string resume tokens to resume a change stream. 无论fcv值是多少,4.0部署都可以使用BinData恢复标记或十六进制字符串恢复标记来恢复更改流。As such, a 4.0 deployment can use a resume token from a change stream opened on a collection from a 3.6 deployment.因此,4.0部署可以使用3.6部署集合上打开的变更流中的恢复令牌。
New resume token formats introduced in a MongoDB version cannot be consumed by earlier MongoDB versions.MongoDB版本中引入的新恢复令牌格式不能被早期的MongoDB使用。
MongoDB provides a "snippet", an extension to MongoDB提供了一个"snippet",它是mongosh
, that decodes hex-encoded resume tokens.mongosh
的扩展,用于解码十六进制编码的恢复令牌。
You can install and run the resumetoken snippet from 您可以从mongosh
:mongosh
安装并运行resumetoken代码段:
snippet install resumetoken
decodeResumeToken('<RESUME TOKEN>')
You can also run resumetoken from the command line (without using 如果系统上安装了mongosh
) if npm
is installed on your system:npm
,也可以从命令行运行resumetoken
(不使用mongosh
):
npx mongodb-resumetoken-decoder <RESUME TOKEN>
See the following for more details on:有关的详细信息,请参阅以下内容:
mongosh
.mongosh
中使用代码段。By default, the change stream cursor returns specific field changes/deltas for update operations. 默认情况下,更改流游标返回更新操作的特定字段更改/增量。You can also configure the change stream to look up and return the current majority-committed version of the changed document. 您还可以配置更改流以查找并返回更改文档的当前大多数提交版本。Depending on other write operations that may have occurred between the update and the lookup, the returned document may differ significantly from the document at the time of the update.根据更新和查找之间可能发生的其他写入操作,返回的文档可能与更新时的文档有很大不同。
Depending on the number of changes applied during the update operation and the size of the full document, there is a risk that the size of the change event document for an update operation is greater than the 16MB BSON document limit. 根据更新操作期间应用的更改数量和完整文档的大小,更新操作的更改事件文档的大小可能大于16MB BSON文档限制。If this occurs, the server closes the change stream cursor and returns an error.如果发生这种情况,服务器将关闭更改流游标并返回错误。
Starting in MongoDB 4.2, change streams are available regardless of the 从MongoDB 4.2开始,无论"majority"
read concern support; that is, read concern majority
support can be either enabled (default) or disabled to use change streams."majority"
读关注点支持如何,都可以使用变更流;也就是说,可以启用(默认)或禁用读关注majority
支持来使用更改流。
In MongoDB 4.0 and earlier, change streams are available only if 在MongoDB 4.0及更早版本中,只有启用了"majority"
read concern support is enabled (default)."majority"
读关注点支持(默认),更改流才可用。
When running with access control, the user must have the 在使用访问控制运行时,用户必须对所有数据库中的所有非系统集合具有find
and changeStream
privilege actions on all non-systems collections across all databases.. find
和changeStream
权限操作。That is, a user must have a role that grants the following privilege:也就是说,用户必须具有授予以下权限的角色:
{ resource: { db: "", collection: "" }, actions: [ "find", "changeStream" ] }
The built-in read
role provides the appropriate privileges.read
读取角色提供适当的权限。
The following operation in mongosh
opens a change stream cursor on a replica set. mongosh
中的以下操作打开副本集上的更改流游标。The returned cursor reports on data changes to all the non-返回的游标报告所有数据库(system
collections across all databases except for the admin
, local
, and the config
databases.admin
、local
和config
数据库除外)中所有非系统集合的数据更改。
watchCursor = db.getMongo().watch()
Iterate the cursor to check for new events. 重复游标以检查新事件。Use the 使用cursor.isExhausted()
method to ensure the loop only exits if the change stream cursor is closed and there are no objects remaining in the latest batch:cursor.isExhausted()
方法确保循环仅在更改流游标关闭且最新批处理中没有剩余对象时存在:
while (!watchCursor.isExhausted()){ if (watchCursor.hasNext()){ printjson(watchCursor.next()); } }
For complete documentation on change stream output, see Change Events.有关更改流输出的完整文档,请参阅更改事件。