"snapshot"

Read Concern 读取关注"snapshot"

On this page本页内容

New in version 4.0.在版本4.0中新增

Changed in version 5.0.在版本5.0中更改

Read concern "snapshot" is available for multi-document transactions, and starting in MongoDB 5.0, certain read operations outside of multi-document transactions.

Outside of multi-document transactions, read concern "snapshot" is available on primaries and secondaries for the following read operations:

All other read commands prohibit "snapshot".

Operations操作

For a list of all operations that accept read concerns, see Operations That Support Read Concern.有关接受读关注的所有操作的列表,请参阅支持读关注的操作

Read Concern and Transactions读取关注点和事务

Multi-document transactions support read concern "snapshot" as well as "local", and "majority".

Note注意

You set the read concern at the transaction level, not at the individual operation level. 您可以在事务级别而不是单个操作级别设置读取关注点。To set the read concern for transactions, see Transactions and Read Concern.要设置事务的读取关注点,请参阅事务和读取关注点

Read Concern and 读取关注和atClusterTime

New in version 5.0.在版本5.0中新增

Outside of multi-document transactions, reads with read concern "snapshot" support the optional parameter atClusterTime. The parameter atClusterTime allows you to specify the timestamp for the read. To satisfy a read request with a specified atClusterTime of T, the mongod performs the request based on the data available at time T.

You can obtain the operationTime or clusterTime of an operation from the response of db.runCommand() or from the Session() object.

The following command performs a find operation with read concern "snapshot" and specifies that the operation should read data from the snapshot at cluster time Timestamp(1613577600, 1).下面的命令使用读取关注点"snapshot"执行查找操作,并指定该操作应在群集Timestamp(1613577600, 1)处从快照读取数据。

db.runCommand( {
    find: "restaurants",
    filter: { _id: 5 },
    readConcern: {
        level: "snapshot",
        atClusterTime: Timestamp(1613577600, 1)
    },
} )

If the parameter atClusterTime is not supplied, the mongos, or in single member replica sets the mongod, selects the timestamp of the latest majority-committed snapshot as the atClusterTime and returns it to the client.

Outside of transactions, "snapshot" reads are guaranteed to read from majority-committed data.在事务之外,"snapshot"读取保证从大多数提交的数据中读取。

atClusterTime Considerations and Behavior考虑因素和行为

  • The allowed values for atClusterTime depend on the minSnapshotHistoryWindowInSeconds parameter. minSnapshotHistoryWindowInSeconds is the minimum time window in seconds for which the storage engine keeps the snapshot history. If you specify an atClusterTime value older than the oldest snapshot retained according to minSnapshotHistoryWindowInSeconds, mongod returns an error.
  • If you perform a read operation with "snapshot" against a delayed replica set member, the returned majority-committed data could be stale.如果对延迟的副本集成员执行带有"snapshot"的读取操作,则返回的多数提交数据可能已过时。
  • It is not possible to specify atClusterTime for "snapshot" inside of causally consistent sessions.

Read Concern on Capped Collections阅读关于封顶集合的关注

Starting in version 5.0, you cannot use read concern "snapshot" when reading from a capped collection.从版本5.0开始,在读取封顶集合时,不能使用读取关注点"snapshot"

←  Read Concern "linearizable"Write Concern →