On this page本页内容
majority
Supportmajority
支持db.collection.watch( pipeline, options )
This is a mongosh
method. This is not the documentation for Node.js
or other programming language specific driver methods.
In most cases, mongosh
methods work the same way as the legacy mongo
shell methods. However, some legacy methods are unavailable in mongosh
.
For the legacy mongo
shell documentation, refer to the documentation for the corresponding MongoDB Server release:
For MongoDB API drivers, refer to the language specific MongoDB driver documentation.有关MongoDB API驱动程序,请参阅特定语言的MongoDB驱动程序文档。
For replica sets and sharded clusters only仅适用于副本集和分片群集
Opens a change stream cursor on the collection.在集合上打开更改流游标。
pipeline | array |
|
options | document | watch() .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 |
|
db.watch()
and 和Mongo.watch()
db.collection.watch()
is available for replica set and sharded cluster deployments :可用于副本集和分片群集部署:
db.collection.watch()
on any data-bearing member.db.collection.watch()
。db.collection.watch()
on a mongos
instance.mongos
实例上发出db.collection.watch()
。You can only use 您只能将db.collection.watch()
with the Wired Tiger storage engine.db.collection.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"
读取关注点支持(默认),更改流才可用。
db.collection.watch()
invalidate
事件;例如集合删除或重命名。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驱动程序在出现某些错误后尝试自动恢复更改流游标。
db.collection.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, db.collection.watch()
cannot resume the change stream.resumeAfter
或startAfter
选项的resume
令牌标识的操作已从oplog中删除,db.collection.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. invalidate
事件(例如,集合删除或重命名)关闭流后,不能使用resumeAfter
恢复更改流。startAfter
在invalidate
事件后启动新的更改流。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 | Feature Compatibility Version | Resume Token _data Type |
---|---|---|
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使用。
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.如果发生这种情况,服务器将关闭更改流游标并返回错误。
When running with access control, the user must have the find
and changeStream
privilege actions on the collection resource. That is, a user must have a role that grants the following privilege:也就是说,用户必须具有授予以下权限的角色:
{ resource: { db: <dbname>, collection: <collection> }, actions: [ "find", "changeStream" ] }
The built-in read
role provides the appropriate privileges.
The following operation opens a change stream cursor against the 以下操作将针对data.sensors
collection:data.sensors
集合打开更改流游标:
watchCursor = db.getSiblingDB("data").sensors.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:
while (!watchCursor.isExhausted()){ if (watchCursor.hasNext()){ printjson(watchCursor.next()); } }
For complete documentation on change stream output, see Change Events.有关变更流输出的完整文档,请参阅变更事件。
Set the 将fullDocument
option to "updateLookup"
to direct the change stream cursor to lookup the most current majority-committed version of the document associated to an update change stream event.fullDocument
选项设置为"updateLookup"
,以指示更改流游标查找与更新更改流事件关联的文档的最新多数提交版本。
The following operation opens a change stream cursor against the data.sensors
collection using the fullDocument : "updateLookup"
option.
watchCursor = db.getSiblingDB("data").sensors.watch( [], { fullDocument : "updateLookup" } )
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:
while (!watchCursor.isExhausted()){ if (watchCursor.hasNext()){ printjson(watchCursor.next()); } }
For any update operation, the change event returns the result of the document lookup in the 对于任何更新操作,更改事件都会在fullDocument
field.fullDocument
字段中返回文档查找的结果。
For an example of the full document update output, see change stream update event.
For complete documentation on change stream output, see Change Events.
Starting in MongoDB 4.2, change streams will throw an exception if the change stream aggregation pipeline modifies an event's _id field.从MongoDB 4.2开始,如果更改流聚合管道修改了事件的_id
字段,则更改流将引发异常。
The following operation opens a change stream cursor against the data.sensors
collection using an aggregation pipeline to filter only insert
events:
watchCursor = db.getSiblingDB("data").sensors.watch( [ { $match : {"operationType" : "insert" } } ] )
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:
while (!watchCursor.isExhausted()){ if (watchCursor.hasNext()){ printjson(watchCursor.next()); } }
The change stream cursor only returns change events where the operationType
is insert
. For complete documentation on change stream output, see Change Events.
Every document returned by a change stream cursor includes a resume token as the 变更流游标返回的每个文档都包含一个_id
field. resume
令牌作为_id
字段。To resume a change stream, pass the entire 要恢复更改流,请将要从中恢复的更改事件的整个_id
document of the change event you want to resume from to either the resumeAfter
or startAfter
option of watch()
._id
文档传递给watch()
的resumeAfter
或startAfter
选项。
The following operation resumes a change stream cursor against the 以下操作使用data.sensors
collection using a resume token. resume
令牌针对data.sensors
集合恢复更改流游标。This assumes that the operation that generated the resume token has not rolled off the cluster's oplog.这假设生成恢复令牌的操作尚未从集群的oplog中滚出。
let watchCursor = db.getSiblingDB("data").sensors.watch(); let firstChange; while (!watchCursor.isExhausted()) { if (watchCursor.hasNext()) { firstChange = watchCursor.next(); break; } } watchCursor.close(); let resumeToken = firstChange._id; resumedWatchCursor = db.getSiblingDB("data").sensors.watch( [], { resumeAfter : resumeToken } )
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 (!resumedWatchCursor.isExhausted()){ if (resumedWatchCursor.hasNext()){ printjson(watchCursor.next()); } }
See Resume a Change Stream for complete documentation on resuming a change stream.有关恢复更改流的完整文档,请参阅恢复更改流。