If you drop or rename a collection or database with change streams opened against it, the change stream cursors close when they advance to that point in the oplog. Change stream cursors with the 如果删除或重命名一个已打开更改流的集合或数据库,则更改流游标在oplog中前进到该点时会关闭。使用fullDocument : updateLookup option may return null for the lookup document.fullDocument : updateLookup选项更改流游标可能会为查找文档返回null。
Change stream response documents must adhere to the 16MB BSON document limit. Depending on the size of documents in the collection against which you open a change stream, notifications may fail if the resulting notification document exceeds the 16MB limit. 变更流响应文档必须符合16MB BSON文档限制。根据您打开更改流所依据的集合中文档的大小,如果生成的通知文档超过16MB的限制,通知可能会失败。For example, update operations on change streams configured to return the full updated document, or insert/replace operations with a document that is at or just below the limit.例如,对配置为返回完整更新文档的更改流进行更新操作,或用等于或略低于限制的文档进行插入/替换操作。
Important
Starting in version 6.0.9, you can use the 从版本6.0.9开始,您可以使用$changeStreamSplitLargeEvent aggregation stage to split the events into smaller fragments.$changeStreamSplitLargeEvent聚合阶段将事件拆分为更小的片段。
Replica Sets副本集
For replica sets with arbiter members, change streams may remain idle if enough data-bearing members are unavailable such that operations cannot be majority committed.对于具有仲裁器成员的副本集,如果有足够的数据承载成员不可用,导致操作无法被多数提交,则更改流可能会保持空闲。
For example, consider a 3-member replica set with two data-bearing nodes and an arbiter. If the secondary goes down, such as due to failure or an upgrade, writes cannot be majority committed. The change stream remains open, but does not send any notifications.例如,考虑一个由3个成员组成的副本集,其中有两个数据承载节点和一个仲裁器。如果辅助服务器发生故障,例如由于故障或升级,则无法提交多数写入。更改流保持打开状态,但不发送任何通知。
In this scenario, the application can catch up to all operations that occurred during the downtime so long as the last operation the application received is still in the oplog of the replica set.在这种情况下,只要应用程序接收到的最后一个操作仍在副本集的oplog中,应用程序就可以赶上停机期间发生的所有操作。
If significant downtime is estimated, such as for an upgrade or a significant disaster, consider increasing the size of the oplog such that operations are retained for a duration of time greater than the estimated downtime. 如果估计有重大停机时间,例如升级或重大灾难,请考虑增加oplog的大小,以便在比估计停机时间更长的时间内保留操作。Use 使用rs.printReplicationInfo() to retrieve information on the oplog status, including the size of the oplog and the time range of operations.rs.printReplicationInfo()检索oplog状态的信息,包括oplog的大小和操作的时间范围。
Sharded Clusters分片集群
Change streams provide a total ordering of changes across shards by utilizing a global logical clock. MongoDB guarantees the order of changes are preserved and change stream notifications can be safely interpreted in the order received. For example, a change stream cursor opened against a 3-shard sharded cluster returns change notifications respecting the total order of those changes across all three shards.变更流通过利用全局逻辑时钟提供跨分片的变更总顺序。MongoDB保证保留更改的顺序,并且可以按照收到的顺序安全地解释更改流通知。例如,在3个分片集群上打开的更改流游标会返回更改通知,这些通知涉及所有三个分片中这些更改的总顺序。
To guarantee total ordering of changes, for each change notification the 为了保证更改的总顺序,对于每个更改通知,mongos checks with each shard to see if the shard has seen more recent changes. mongos都会检查每个分片,看看分片是否看到了最近的更改。Sharded clusters with one or more shards that have little or no activity for the collection, or are "cold", can negatively affect the response time of the change stream as the 具有一个或多个对集合几乎没有活动或“冷”的分片的分片集群会对更改流的响应时间产生负面影响,因为mongos must still check with those cold shards to guarantee total ordering of changes. mongos仍然必须检查这些冷分片以确保更改的总顺序。This effect may be more apparent with geographically distributed shards, or workloads where the majority of operations occur on a subset of shards in the cluster. 这种影响在地理上分布的分片或大多数操作发生在集群中的一个子集上的工作负载中可能更为明显。To minimize latency for cold shards, you can specify a lower 为了尽量减少冷分片的延迟,您可以指定一个较低的periodicNoopIntervalSecs value.periodicNoopIntervalSecs值。
If a sharded collection has high levels of activity, the 如果一个分片集合的活动水平很高,mongos may not be able to keep up with the changes across all of the shards. mongos可能无法跟上所有分片的变化。Consider utilizing notification filters for these types of collections. For example, passing a 考虑对这些类型的集合使用通知筛选器。例如,传递一个配置为仅筛选$match pipeline configured to filter only insert operations.insert操作的$match管道。
Indexes and Performance指标和性能
Change streams cannot use indexes. MongoDB does not support creating indexes on the oplog collection. Therefore, avoid opening a high number of specifically-targeted change streams as these can impact server performance.更改流不能使用索引。MongoDB不支持在oplog集合上创建索引。因此,避免打开大量专门针对的更改流,因为这些更改流会影响服务器性能。
Change Stream Optimization更改流优化
Starting in MongoDB 5.1, change streams are optimized, providing more efficient resource utilization and faster execution of some aggregation pipeline stages.从MongoDB 5.1开始,更改流得到了优化,提供了更高效的资源利用率和更快地执行某些聚合管道阶段。
Change Streams and Orphan Documents更改流和孤立文档
Starting in MongoDB 5.3, during range migration, change stream events are not generated for updates to orphaned documents.从MongoDB 5.3开始,在范围迁移期间,不会为孤立文档的更新生成更改流事件。