对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

Mongo.startSession()(mongosh方法)

Mongo.startSession(<options>)

Starts a session for the connection. mongosh assigns the session ID to commands associated with the session.

重要

mongosh 方法

This page documents a mongosh method. This is not the documentation for database commands or language-specific drivers, such as Node.js.

有关数据库命令,请参阅 startSession 命令。

如需了解 MongoDB API 驱动程序,请参阅特定语言的 MongoDB 驱动程序文档。

会话只能与创建会话的 MongoClient 对象一起使用。单个会话不能同时使用。使用单个会话的操作必须按顺序运行。

The startSession() method can take a document with session options. The options available are:

字段
说明

因果一致性(Causal Consistency)

Boolean. Enables or disables causal consistency for the session. Mongo.startSession() enables causalConsistency by default. Mutually exclusive with snapshot.

启动会话后,无法修改其 causalConsistency 设置。

即使 Mongo() 连接对象禁用了因果一致性,会话仍可能启用了因果一致性,反之亦然。要设置连接对象的因果一致性,请参见 Mongo.setCausalConsistency()

事务外的

文档。指定读关注

要在启动会话后修改设置,请参阅 Session.getOptions().setReadConcern()

readPreference

文档。指定读取偏好

readPreference 文档包含 mode 字段和可选的 tags 字段:

{ mode: <string>, tags: <array> }

要在启动会话后修改设置,请参阅 Session.getOptions().setReadPreference()

retryWrites

布尔值。启用或禁用遇到瞬时网络错误时重试写入的功能。

If you start mongosh with the --retryWrites option, retryWrites is enabled by default for Mongo.startSession().

启动会话后,无法修改其 retryWrites 设置。

snapshot

布尔值。 为MongoDB 5.0 + 部署启用会话快照读取。 与causalConsistency互斥。

writeConcern

文档。指定写关注

要在启动会话后修改设置,请参阅 Session.getOptions().setWriteConcern()

此方法可用于以下环境中托管的部署:

  • MongoDB Atlas:用于云中 MongoDB 部署的完全托管服务

注意

所有 MongoDB Atlas 集群都支持此命令。有关 Atlas 对所有命令的支持的信息,请参阅不支持的命令

The following starts a session with causal consistency and retryable writes enabled on the Mongo() connection object associated with mongosh's global db variable:

db = db.getMongo().startSession({retryWrites: true, causalConsistency: true}).getDatabase(db.getName());
给本页内容打分

在此页面上