- MongoDB官方文档中文版
- MongoDB中文手册说明
- MongoDB简介
- 安装 MongoDB
- The mongo Shell
- MongoDB CRUD 操作
- 聚合
- 数据模型
- 事务
- 索引
- 安全
- 安全检查列表
- 启用访问控制
- 身份验证
- 基于角色的访问控制
- TLS / SSL(传输加密)
- 静态加密
- 客户端字段级加密
- 审计
- 网络和配置强化
- 实现字段级别修订
- 安全参考
- 附录
- 变更流
- 复制
- 分片
- 分片键
- 哈希分片
- 范围分片
- 区
- 管理分片区
- 按位置细分数据
- 用于更改SLA或SLO的分层硬件
- 按应用或客户细分数据
- 仅插入工作负载的分布式本地写入
- 管理分片区
- 使用块进行数据分区
- 在分片集群中拆分数据块
- 管理
- 存储
- 存储引擎
- 日志记录
- 管理日志记录
- GridFS
- FAQ:MongoDB 存储
- 存储引擎
- 参考
- 运算符
- 查询与映射运算符
- 更新运算符
- 聚合管道阶段
- 聚合管道操作符
- $abs (aggregation)
- $acos (aggregation)
- $acosh (aggregation)
- $add (aggregation)
- $addToSet (aggregation)
- $allElementsTrue (aggregation)
- $and (aggregation)
- $anyElementTrue (aggregation)
- $arrayElemAt (aggregation)
- $arrayToObject (aggregation)
- $asin (aggregation)
- $asinh (aggregation)
- $atan (aggregation)
- $atan2 (aggregation)
- $atanh (aggregation)
- $avg (aggregation)
- $ceil (aggregation)
- $cmp (aggregation)
- $concat (aggregation)
- $concatArrays (aggregation)
- $cond (aggregation)
- $convert (aggregation)
- $cos (aggregation)
- $dateFromParts (aggregation)
- $dateToParts (aggregation)
- $dateFromString (aggregation)
- $literal (aggregation)
- 查询修饰符
- 数据库命令
- 聚合命令
- 地理空间命令
- 查询和写操作命令
- 查询计划缓存命令
- 认证命令
- 用户管理命令
- 角色管理命令
- 复制命令
- 分片命令
- 会话命令
- 管理命令
- 诊断命令
- 免费监控命令
- 系统事件审计命令
- mongo Shell 方法
- 集合方法
- db.collection.aggregate()
- db.collection.bulkWrite()
- db.collection.copyTo()
- db.collection.count()
- db.collection.countDocuments()
- db.collection.estimatedDocumentCount()
- db.collection.createIndex()
- db.collection.createIndexes()
- db.collection.dataSize()
- db.collection.deleteOne()
- db.collection.deleteMany()
- db.collection.distinct()
- db.collection.drop()
- db.collection.dropIndex()
- db.collection.dropIndexes()
- db.collection.ensureIndex()
- db.collection.explain()
- db.collection.find()
- db.collection.findAndModify()
- db.collection.findOne()
- db.collection.findOneAndDelete()
- db.collection.findOneAndReplace()
- db.collection.findOneAndUpdate()
- db.collection.getIndexes()
- db.collection.getShardDistribution()
- db.collection.getShardVersion()
- db.collection.insert()
- db.collection.insertOne()
- db.collection.insertMany()
- db.collection.isCapped()
- db.collection.latencyStats()
- db.collection.mapReduce()
- db.collection.reIndex()
- db.collection.remove()
- db.collection.renameCollection()
- db.collection.replaceOne()
- db.collection.save()
- db.collection.stats()
- db.collection.storageSize()
- db.collection.totalIndexSize()
- db.collection.totalSize()
- db.collection.update()
- db.collection.updateOne()
- db.collection.updateMany()
- db.collection.watch()
- db.collection.validate()
- 词汇表
- 默认的MongoDB端口
- 默认的MongoDB读/写关注
- 服务器会话
- MongoDB驱动
- FAQ
- 联系我们
- 更多资料
- [快学Mongo]
- [Mongo问题讨论区]
- [Mongo 驱动使用手册]
- 本书使用 GitBook 发布
变更事件
Change Events
在本页面
Change Events
insert Event
update Event
replace Event
delete Event
drop Event
rename Event
dropDatabase Event
invalidate Event
Change Events
以下文档表示变更流响应文档可以具有的所有可能的字段。
{
_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>", ... ]
}
"clusterTime" : <Timestamp>,
"txnNumber" : <NumberLong>,
"lsid" : {
"id" : <UUID>,
"uid" : <BinData>
}
}
有些字段仅适用于某些操作,例如更新。下表描述了变更流响应文档中的每个字段:
Field | Type | Description |
---|---|---|
_id | document | Metadata related to the operation. Acts as the resumeToken for the resumeAfter parameter when resuming a change stream.copycopied{ "_data" : <BinData</td> <td>hex string> } <code>The</code>_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. For details, see Resume Tokens. |
operationType | string | The type of operation that occurred. Can be any of the following values:insert |
fullDocument | document | The document created or modified by the insert , replace , delete , update operations (i.e. CRUD operations).For insert and replace operations, this represents the new document created by the operation.For delete operations, this field is omitted as the document no longer exists.For update operations, this field only appears if you configured the change stream with fullDocument set to updateLookup . This field then represents the most current majority-committed version of the document modified by the update operation. This document may differ from the changes described in updateDescription if other majority-committed operations modified the document between the original update operation and the full document lookup. |
ns | document | The namespace (database and or collection) affected by the event. |
ns.db | string | The name of the database. |
ns.coll | string | The name of the collection.For dropDatabase operations, this field is omitted. |
to | document | When operationType : rename , this document displays the new name for the ns collection. This document is omitted for all other values of operationType . |
to.db | string | The new name of the database. |
to.coll | string | The new name of the collection. |
documentKey | document | A document that contains the _id of the document created or modified by the insert , replace , delete , update operations (i.e. CRUD operations). For sharded collections, also displays the full shard key for the document. The _id field is not repeated if it is already a part of the shard key. |
updateDescription | document | A document describing the fields that were updated or removed by the update operation.This document and its fields only appears if the operationType is update . |
updateDescription.updatedFields | document | A document whose keys correspond to the fields that were modified by the update operation. The value of each field corresponds to the new value of those fields, rather than the operation that resulted in the new value. |
updateDescription.removedFields | array | An array of fields that were removed by the update operation. |
clusterTime | Timestamp | The timestamp from the oplog entry associated with the event.For events that happened as part of a multi-document transaction, the associated change stream notifications will have the same clusterTime value, namely the time when the transaction was committed.On a sharded cluster, events that occur on different shards can have the same clusterTime but be associated with different transactions or even not be associcated with any transaction. To identify events for a single transaction, you can use the combination of lsid and txnNumber in the change stream event document.New in version 4.0. |
txnNumber | NumberLong | The transaction number.Only present if the operation is part of a multi-document transaction.New in version 4.0. |
lsid | Document | The identifier for the session associated with the transaction.Only present if the operation is part of a multi-document transaction.New in version 4.0. |
insert事件
以下示例说明了一个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'
}
}
该documentKey字段包括_id和userName 字段。这表示engineering.users集合已分片,并且在userName和上都有分片键_id。
该fullDocument文档表示插入时文档的版本。
update事件
以下示例说明了一个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']
}
}
以下示例说明了update使用选项打开的变更流的事件:fullDocument : updateLookup
{
_id: { < Resume Token > },
operationType: 'update',
clusterTime: <Timestamp>,
ns: {
db: 'engineering',
coll: 'users'
},
documentKey: {
_id: ObjectId("58a4eb4a30c75625e00d2820")
},
updateDescription: {
updatedFields: {
email: 'alice@10gen.com'
},
removedFields: ['phoneNumber']
},
fullDocument: {
_id: ObjectId("58a4eb4a30c75625e00d2820"),
name: 'Alice',
userName: 'alice123',
email: 'alice@10gen.com',
team: 'replication'
}
}
该fullDocument文档代表了更新文档的最新多数批准版本。该fullDocument文档可能与更新操作时的文档有所不同,具体取决于在更新操作和文档查找之间发生的交错多数授权操作的数量。
replace事件
以下示例说明了一个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'
}
}
一个replace操作使用update命令,并且包括两个阶段:
使用documentKey和删除原始文档 使用相同的插入新文档 documentkey 在fullDocument一个的replace事件表示替换文件的插入后的文件。
delete事件
以下示例说明了一个delete事件:
{
_id: { < Resume Token > },
operationType: 'delete',
clusterTime: <Timestamp>,
ns: {
db: 'engineering',
coll: 'users'
},
documentKey: {
_id: ObjectId("599af247bb69cd89961c986d")
}
}
该fullDocument文档被省略,因为在更改流游标将delete事件发送到客户端时,该文档不再存在。
drop事件
版本4.0.1中的新功能。
一个drop在集合从数据库中删除发生的事件。以下示例说明了一个drop事件:
{
_id: { < Resume Token > },
operationType: 'drop',
clusterTime: <Timestamp>,
ns: {
db: 'engineering',
coll: 'users'
}
}
一个drop事件导致一个无效事件 变革流张开攻击它的ns集合。
rename事件
版本4.0.1中的新功能。
一个rename在集合重命名发生的事件。以下示例说明了一个rename事件:
{
_id: { < Resume Token > },
operationType: 'rename',
clusterTime: <Timestamp>,
ns: {
db: 'engineering',
coll: 'users'
},
to: {
db: 'engineering',
coll: 'people'
}
}
一个rename事件导致一个 无效事件的流变化对打开的ns集合或to集合。
dropDatabase事件
版本4.0.1中的新功能。
一个dropDatabase当数据库被丢弃发生的事件。以下示例说明了一个dropDatabase事件:
{
_id: { < Resume Token > },
operationType: 'dropDatabase',
clusterTime: <Timestamp>,
ns: {
db: 'engineering'
}
}
一个dropDatabase事件导致一个 无效事件的流变化对打开的ns.db数据库。
invalidate事件
以下示例说明了一个invalidate事件:
{
_id: { < Resume Token > },
operationType: 'invalidate',
clusterTime: <Timestamp>
}
对于针对集合打开的变更流,影响监视的集合的 放置事件, 重命名事件或 dropDatabase事件导致 无效事件。
对于针对数据库打开的变更流,影响受监视数据库的 dropDatabase事件将导致 invalidate事件。
invalidate 事件关闭更改流游标。
resumeAfter在无效事件(例如,集合删除或重命名)关闭流之后,您不能用来恢复更改 流。从MongoDB 4.2开始,您可以使用 startAfter在invalidate事件之后启动新的更改流。
译者:wh
Copyright © 上海锦木信息技术有限公司 all right reserved,由 MongoDB汉化小组 提供技术支持文件修订时间: 2020-10-11 20:53:05