$changeStream (aggregation)
On this page本页内容
Definition定义
$changeStream-
Returns a Change Stream cursor on a collection, a database, or an entire cluster. Must be used as the first stage in an aggregation pipeline.返回集合、数据库或整个集群上的更改流游标。必须用作聚合管道中的第一个阶段。The$changeStreamstage has the following syntax:$changeStream阶段具有以下语法:{
$changeStream: {
allChangesForCluster: <boolean>,
fullDocument: <string>,
fullDocumentBeforeChange: <string>,
resumeAfter: <int>
showExpandedEvents: <boolean>,
startAfter: <document>
startAtOperationTime: <timestamp>
}
}Parameter参数Description描述allChangesForClusterOptional:可选的。Sets whether the change stream should include all changes in the cluster. May only be opened on the设置更改流是否应包括集群中的所有更改。只能在admindatabase.admin数据库上打开。fullDocumentOptional:可选的。Specifies whether change notifications include a copy of the full document when modified by指定在通过updateoperations.update操作进行修改时,更改通知是否包括完整文档的副本。default: Change notifications do not include the full document for:更改通知不包括用于updateoperations.update操作的完整文档。required: Change notifications includes a copy of the modified document as it appeared immediately after the change.:更改通知包括更改后立即出现的修改文档的副本。If the document cannot be found, the change stream throws an error.如果找不到文档,更改流将抛出一个错误。
To use this option, you must first use the要使用此选项,必须首先使用collModcommand to enable thechangeStreamPreAndPostImagesoption.collMod命令启用changeStreamPreAndPostImages选项。
New in version 6.0.6.0版新增。
updateLookup: Change notifications includes a copy of the document modified by the change.:更改通知包括更改所修改的文档的副本。This document is the current majority-committed document or此文档是当前多数提交的文档,如果已不存在,则为nullif it no longer exists.null。whenAvailable: Change notification includes a copy of the modified document as it appeared immediately after the change or:更改通知包括更改后立即出现的修改文档的副本,如果文档不可用,则为nullif the document is unavailable.null。
To use this option, you must first use the要使用此选项,必须首先使用collModcommand to enable thechangeStreamPreAndPostImagesoption.collMod命令启用changeStreamPreAndPostImages选项。
New in version 6.0.6.0版新增。
In the case of partial updates, the change notification also provides a description of the change.在部分更新的情况下,更改通知还提供对更改的描述。fullDocumentBeforeChangeInclude the full document from before the change.包括更改前的完整文档。This field accepts the following values:此字段接受以下值:off: Disables inclusion of the document from before the change.:禁止包含更改前的文档。whenAvailable: Includes document from before the change.:包括更改前的文档。The query does not fail if the unmodified document is not available.如果未修改的文档不可用,则查询不会失败。required: Includes document from before the change. The query fails if the unmodified document is not available.:包括更改前的文档。如果未修改的文档不可用,则查询失败。
resumeAfterSpecifies a resume token as the logical starting point for the change stream.指定恢复令牌作为更改流的逻辑起点。Cannot be used with不能与startAfterorstartAtOperationTimefields.startAfter或startAtOperationTime字段一起使用。showExpandedEventsSpecifies whether to include additional change events, such as such as DDL and index operations.指定是否包括其他更改事件,例如DDL和索引操作。
New in version 6.0.6.0版新增。startAfterSpecifies a resume token as the logical starting point for the change stream.指定恢复令牌作为更改流的逻辑起点。Cannot be used with不能与resumeAfterorstartAtOperationTimefields.resumeAfter或startAtOperationTime字段一起使用。startAtOperationTimeSpecifies a time as the logical starting point for the change stream.指定一个时间作为更改流的逻辑起点。Cannot be used with不能与resumeAfterorstartAfterfields.resumeAfter或startAfter字段一起使用。
Examples实例
To create a change stream cursor using the aggregation stage, run the 要使用聚合阶段创建更改流游标,请运行aggregate command.aggregate命令。
var cur = db.names.aggregate( [
{ $changeStream: {} }
] )
To open the cursor, run 要打开游标,请运行cur.cur。
When the change stream detects a change, the 当变更流检测到变更时,next() method returns a change event notification. next()方法返回一个变更事件通知。For example, after running 例如,在运行cur.next(), MongoDB returns a document similar to the following:cur.next()后,MongoDB返回一个类似于以下内容的文档:
{
"_id": {
_data: "8262E2EE54000000022B022C0100296E5A100448E5E3DD01364019AE8FE8C6859527E046645F6964006462E2EE54C8756C0D5CF6F0720004"
},
"operationType": "insert",
"clusterTime": Timestamp({ t: 1659039316, i: 2 }),
"wallTime": ISODate("2022-07-28T20:15:16.148Z"),
"fullDocument": {
"_id": ObjectId("62e2ee54c8756c0d5cf6f072"),
"name": "Walker Percy"
},
"ns": {
"db": "test",
"coll": "names"
},
"documentKey": { _id: ObjectId("62e2ee54c8756c0d5cf6f072") }
}
For more information on change stream notifications, see Change Events.有关更改流通知的更多信息,请参阅更改事件。