On this page本页内容
The following document represents all possible fields that a change stream response document can have.以下文档表示变更流响应文档可以具有的所有可能字段。
{
_id : { <BSON Object> },
"operationType" : "<operation>",
"fullDocument" : { <document> },
"ns" : {
"db" : "<database>",
"coll" : "<collection>"
},
"to" : {
"db" : "<database>",
"coll" : "<collection>"
},
"documentKey" : { "_id" : <value> },
"updateDescription" : {
"updatedFields" : { <document> },
"removedFields" : [ "<field>", ... ],
"truncatedArrays" : [
{ "field" : <field>, "newSize" : <integer> },
...
]
},
"clusterTime" : <Timestamp>,
"txnNumber" : <NumberLong>,
"lsid" : {
"id" : <UUID>,
"uid" : <BinData>
}
}
Some fields are only available for certain operations, such as updates. 某些字段仅适用于某些操作,例如更新。The following table describes each field in the change stream response document:下表描述了变更流响应文档中的每个字段:
_id | document |
{
"_data" : <BinData|hex string>
}
|
operationType | string |
|
fullDocument | document |
|
ns | document | |
ns.db | string | |
ns.coll | string |
|
to | document | operationType : rename, this document displays the new name for the ns collection. operationType:rename时,此文档显示ns集合的新名称。operationType. operationType的所有其他值都省略了此文档。 |
to.db | string | |
to.coll | string | |
documentKey | document | _id of the document created or modified by the insert, replace, delete, update operations (i.e. CRUD operations). insert、replace、delete、update操作(即CRUD操作)创建或修改的文档的_id的文档。_id field is not repeated if it is already a part of the shard key. _id字段已经是分片键的一部分,则不会重复该字段。 |
updateDescription | document |
|
updateDescription.updatedFields | document | |
updateDescription.removedFields | array | |
updateDescription.truncatedArrays | array |
|
updateDescription.truncatedArrays.field | string | |
updateDescription.truncatedArrays.newSize | integer | |
clusterTime | Timestamp |
|
txnNumber | NumberLong |
|
lsid | Document |
|
insertThe following example illustrates an 以下示例演示了insert event:insert事件:
{
_id: { < Resume Token > },
operationType: 'insert',
clusterTime: <Timestamp>,
ns: {
db: 'engineering',
coll: 'users'
},
documentKey: {
userName: 'alice123',
_id: ObjectId("599af247bb69cd89961c986d")
},
fullDocument: {
_id: ObjectId("599af247bb69cd89961c986d"),
userName: 'alice123',
name: 'Alice'
}
}
The documentKey field includes both the _id and the userName field. documentKey字段包括_id和userName字段。This indicates that the 这表明engineering.users collection is sharded, with a shard key on userName and _id.engineeringusers集合是分片的,在userName和_id上有一个分片键。
The fullDocument document represents the version of the document at the time of the insert.fullDocument文档表示插入时文档的版本。
updateThe following example illustrates an 以下示例说明了一个update event:update事件:
{
_id: { < Resume Token > },
operationType: 'update',
clusterTime: <Timestamp>,
ns: {
db: 'engineering',
coll: 'users'
},
documentKey: {
_id: ObjectId("58a4eb4a30c75625e00d2820")
},
updateDescription: {
updatedFields: {
email: 'alice@10gen.com'
},
removedFields: ['phoneNumber'],
truncatedArrays: [ {
"field" : "vacation_time",
"newSize" : 36
} ]
}
}
The following example illustrates an 以下示例说明了使用update event for change streams opened with the fullDocument : updateLookup option:fullDocument:updateLookup选项打开的更改流的update事件:
{
_id: { < Resume Token > },
operationType: 'update',
clusterTime: <Timestamp>,
ns: {
db: 'engineering',
coll: 'users'
},
documentKey: {
_id: ObjectId("58a4eb4a30c75625e00d2820")
},
updateDescription: {
updatedFields: {
email: 'alice@10gen.com'
},
removedFields: ['phoneNumber'],
truncatedArrays: [ {
"field" : "vacation_time",
"newSize" : 36
} ]
},
fullDocument: {
_id: ObjectId("58a4eb4a30c75625e00d2820"),
name: 'Alice',
userName: 'alice123',
email: 'alice@10gen.com',
team: 'replication'
}
}
The fullDocument document represents the most current majority-committed version of the updated document. fullDocument文档代表更新文档的最新多数提交版本。The fullDocument document may vary from the document at the time of the update operation depending on the number of interleaving majority-committed operations that occur between the update operation and the document lookup.fullDocument文档可能与更新操作时的文档有所不同,具体取决于在更新操作和文档查找之间发生的交叉多数提交操作的数量。
replaceThe following example illustrates a 以下示例说明了replace event:replace事件:
{
_id: { < Resume Token > },
operationType: 'replace',
clusterTime: <Timestamp>,
ns: {
db: 'engineering',
coll: 'users'
},
documentKey: {
_id: ObjectId("599af247bb69cd89961c986d")
},
fullDocument: {
_id: ObjectId("599af247bb69cd89961c986d"),
userName: 'alice123',
name: 'Alice'
}
}
A replace operation uses the update command, and consists of two stages:replace操作使用更新命令,由两个阶段组成:
documentKey anddocumentKey和documentkeydocumentkey插入新文档The 替换事件的fullDocument of a replace event represents the document after the insert of the replacement document.fullDocument表示插入replace文档后的文档。
deleteThe following example illustrates a 以下示例说明了一个delete event:delete事件:
{
_id: { < Resume Token > },
operationType: 'delete',
clusterTime: <Timestamp>,
ns: {
db: 'engineering',
coll: 'users'
},
documentKey: {
_id: ObjectId("599af247bb69cd89961c986d")
}
}
The 当变更流游标向客户机发送删除事件时,由于文档不再存在,因此省略了fullDocument document is omitted as the document no longer exists at the time the change stream cursor sends the delete event to the client.fullDocument文档。
dropNew in version 4.0.1.在版本4.0.1中新增。
A 当从数据库中删除集合时,会发生drop event occurs when a collection is dropped from a database. drop事件。The following example illustrates a 以下示例说明了一个drop event:drop事件:
{
_id: { < Resume Token > },
operationType: 'drop',
clusterTime: <Timestamp>,
ns: {
db: 'engineering',
coll: 'users'
}
}
A drop event leads to an invalidate event for change streams opened against its ns collection.drop事件会导致针对其ns集合打开的更改流的失效事件。
renameNew in version 4.0.1.在版本4.0.1中新增。
A 重命名集合时会发生rename event occurs when a collection is renamed. rename事件。The following example illustrates a 以下示例说明了rename event:rename事件:
{
_id: { < Resume Token > },
operationType: 'rename',
clusterTime: <Timestamp>,
ns: {
db: 'engineering',
coll: 'users'
},
to: {
db: 'engineering',
coll: 'people'
}
}
A rename event leads to an invalidate event for change streams opened against its ns collection or to collection.rename事件会导致针对其ns集合或到集合打开的更改流的无效事件。
dropDatabaseNew in version 4.0.1.在版本4.0.1中新增。
A 删除数据库时会发生dropDatabase event occurs when a database is dropped. dropDatabase事件。The following example illustrates a 以下示例演示了dropDatabase event:dropDatabase事件:
{
_id: { < Resume Token > },
operationType: 'dropDatabase',
clusterTime: <Timestamp>,
ns: {
db: 'engineering'
}
}
A dropDatabase command generates a drop event for each collection in the database before generating a dropDatabase event for the database.dropDatabase命令为数据库中的每个集合生成一个drop事件,然后再为数据库生成drop数据库事件。
A dropDatabase event leads to an invalidate event for change streams opened against its ns.db database.dropDatabase事件会导致针对其nsdb数据库打开的更改流的无效事件。
invalidateThe following example illustrates an 以下示例说明了一个invalidate event:invalidate事件:
{
_id: { < Resume Token > },
operationType: 'invalidate',
clusterTime: <Timestamp>
}
For change streams opened up against a collection, a drop event, rename event, or dropDatabase event that affects the watched collection leads to an invalidate event.对于针对集合打开的更改流,影响监视的集合的drop事件、rename事件或dropDatabase事件将导致无效事件。
For change streams opened up against a database, a dropDatabase event that affects the watched database leads to an invalidate event.对于针对数据库打开的更改流,影响关注的数据库的dropDatabase事件将导致无效事件。
invalidate events close the change stream cursor.事件关闭变更流游标。
You cannot use 在无效事件(例如,集合删除或重命名)关闭更改流后,不能使用resumeAfter to resume a change stream after an invalidate event (for example, a collection drop or rename) closes the stream. resumeAfter恢复更改流。Starting in MongoDB 4.2, you can use startAfter to start a new change stream after an invalidate event.从MongoDB 4.2开始,您可以使用startAfter在失效事件后启动新的更改流。