Definition定义
cursor.allowDiskUse()-
Important
mongosh
Method方法This page documents a本页记录了一种mongoshmethod. This is not the documentation for a language-specific driver, such as Node.js.mongosh方法。这不是针对特定语言驱动程序(如Node.js)的文档。For MongoDB API drivers, refer to the language-specific MongoDB driver documentation.有关MongoDB API驱动程序,请参阅特定语言的MongoDB驱动程序文档。Use当管道阶段超过100 MB限制时,使用allowDiskUse()to either allow or prohibit writing temporary files on disk when a pipeline stage exceeds the 100 megabyte limit.allowDiskUse()允许或禁止在磁盘上写入临时文件。Starting in MongoDB 6.0, operations that require greater than 100 megabytes of memory automatically write data to temporary files by default.从MongoDB 6.0开始,默认情况下,需要大于100兆字节内存的操作会自动将数据写入临时文件。allowDiskUse()has the following form:具有以下形式:db.collection.find(<match>).sort(<sort>).allowDiskUse()
See Sort and Index Use for more information on in-memory sort operations.有关内存中排序操作的更多信息,请参阅排序和索引使用。
Compatibility兼容性
This method is available in deployments hosted in the following environments:此方法在以下环境中托管的部署中可用:
- MongoDB Atlas
: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务
Note
This command is supported in all MongoDB Atlas clusters. 所有MongoDB Atlas集群都支持此命令。For information on Atlas support for all commands, see Unsupported Commands.有关Atlas支持所有命令的信息,请参阅不支持的命令。
- MongoDB Enterprise
: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本 - MongoDB Community
: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本
Behavior行为
Interaction with allowDiskUseByDefault与allowDiskUseByDefault的交互
allowDiskUseByDefaultStarting in MongoDB 6.0, pipeline stages that require more than 100 megabytes of memory to execute write temporary files to disk by default.从MongoDB 6.0开始,默认情况下,需要超过100兆字节内存来执行将临时文件写入磁盘的流水线阶段。
Note
Prior to MongoDB 6.0, 在MongoDB 6.0之前,.allowDiskUse(false) and .allowDiskUse(true) have the same effect. In MongoDB 6.0, both mongosh and the legacy mongo shell behave the following way:.allowDiskUse(false)和.allowDiskUse(true)具有相同的效果。在MongoDB 6.0中,mongosh和遗留mongo shell的行为如下:
If 如果allowDiskUseByDefault is true (this is the default):allowDiskUseByDefault为true(这是默认值):
.allowDiskUse()has no additional effect没有额外效果.allowDiskUse(true)has no additional effect没有额外效果.allowDiskUse(false)prohibits the query from writing temporary files to disk禁止查询将临时文件写入磁盘
If allowDiskUseByDefault is false:
.allowDiskUse()enables writing temporary files to disk允许将临时文件写入磁盘.allowDiskUse(true)enables writing temporary files to disk允许将临时文件写入磁盘.allowDiskUse(false)has no additional effect没有额外效果
Supports Large Non-Indexed Sorts Only仅支持大型非索引排序
cursor.allowDiskUse() has no effect on sort operations answered using an index or non-indexed ("in-memory") sort operations which require less than 100 megabytes of memory. 对使用索引或非索引(“内存中”)排序操作回答的排序操作没有影响,这些操作需要少于100兆字节的内存。For more complete documentation on in-memory sorts and sort index use, see Sort and Index Use.有关内存中排序和排序索引使用的更完整文档,请参阅排序和索引使用。
To check if MongoDB must perform an in-memory sort, append 要检查MongoDB是否必须执行内存排序,请将cursor.explain() to the query and check the explain results. cursor.explain()附加到查询中并检查解释结果。If the query plan contains a 如果查询计划包含SORT stage, then MongoDB must perform an in-memory sort operation subject to the 100 megabyte memory limit.SORT阶段,则MongoDB必须执行内存中的排序操作,但受100兆字节内存限制。
Example示例
Consider a collection 考虑一个只有sensors with only the default index on _id. The collection contains documents similar to the following:_id默认索引的集合sensors(传感器)。该集合包含类似于以下内容的文档:
{
"sensor-name" : "TEMP-21425",
"sensor-location" : "Unit 12",
"reading" : {
"timestamp" : Timestamp(1580247215, 1),
"value" : 212,
"unit" : "Fahrenheit"
}
}
The following operation includes a 以下操作包括在cursor.sort() on the field reading.timestamp. reading.timestamp字段上使用cursor.sort()。The operation also passes 该操作还将false to cursor.allowDiskUse() to prohibit the query from writing temporary files to disk.false传递给cursor.allowDiskUse(),以禁止查询将临时文件写入磁盘。
db.sensors.find({"sensor-location" : "Unit 12"}).
sort({"reading.timestamp" : 1}).
allowDiskUse(false)
Since 由于reading.timestamp is not included in an index, MongoDB must perform an in-memory sort operation to return results in the requested sort order. reading.timestamp不包含在索引中,MongoDB必须执行内存中的排序操作,以按请求的排序顺序返回结果。By specifying 通过指定cursor.allowDiskUse(false), MongoDB cannot process the sort operation if it requires more than 100 megabytes of system memory. cursor.allowDiskUse(false),如果排序操作需要超过100兆字节的系统内存,MongoDB将无法处理排序操作。If the operation requires more than 100 megabytes of system memory, MongoDB would return an error.如果操作需要超过100兆字节的系统内存,MongoDB将返回错误。