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

游标()(mongosh方法)

cursor.addOption(flag)

重要

mongosh 方法

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

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

注意

自 v3.2 起已弃用

从 v3.2 开始, mongo中已弃用cursor.addOption()操作符。 请改用可用的游标方法

用于通过设置下面列出的标志来更改查询行为。

cursor.addOption()方法具有以下参数:

Parameter
类型
说明

flag

flag

For mongosh, you can use the cursor flags listed below. For the driver-specific list, see your driver documentation.

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

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

重要

所有MongoDB Atlas集群都对该命令提供有限支持。有关更多信息,请参阅不支持的命令。

mongosh 提供了几个附加的游标标志来修改游标的行为。

标记
说明
DBQuery.Option.tailable

将游标设置为在收到最后一个数据后不关闭,从而允许查询继续返回在初始结果用完后添加的数据。

DBQuery.Option.slaveOk

允许查询副本辅助节点。

DBQuery.Option.noTimeout

防止服务器空闲游标超时。

DBQuery.Option.awaitData

For use with DBQuery.Option.tailable. Sets the cursor to block the query thread when no data is available and await data for a set time instead of immediately returning no data. The cursor returns no data only if the timeout expires.

DBQuery.Option.exhaust

将游标设置为一次返回查询返回的所有数据,而不是将结果拆分为多个批处理。

DBQuery.Option.partial

将游标设置为从针对分片集群的查询中返回部分数据,其中某些分片不响应而不是引发错误。

以下示例添加了DBQuery.Option.tailable标志和DBQuery.Option.awaitData标志,以确保查询返回可追加游标。该序列创建一个游标。 返回完整结果集后,它会等待默认的 1000 毫秒间隔,以便捕获并返回查询期间添加的其他数据:

var t = db.myCappedCollection;
var cursor = t.find().addOption(DBQuery.Option.tailable).
addOption(DBQuery.Option.awaitData)

警告

添加不正确的传输协议标志可能会导致问题和/或额外的服务器负载。

给本页内容打分