cursor.allowDiskUse()
On this page本页内容
Definition定义
New in version 4.4. 4.4版新增。
cursor.allowDiskUse()
- Important
mongosh Method
This page documents a
mongosh
method. This is not the documentation for a language-specific driver, such as Node.js.For MongoDB API drivers, refer to the language-specific MongoDB driver documentation.
Use当管道阶段超过100兆字节的限制时,使用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开始,默认情况下,需要超过100MB内存的操作会自动将数据写入临时文件。allowDiskUse()
has the following form:具有以下形式:db.collection.find(<match>).sort(<sort>).allowDiskUse()
See Sort and Index Use for more information on blocking sort operations.有关阻止排序操作的详细信息,请参阅排序和索引使用。
Behavior行为
Interaction with allowDiskUseByDefault
与allowDiskUseByDefault
的交互
allowDiskUseByDefault
Starting 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开始,需要超过100MB内存才能执行的管道阶段默认情况下会将临时文件写入磁盘。
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
:allowDiskUseByDefault
为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 ("blocking") sort operations which require less than 100 megabytes of memory. 对使用索引回答的排序操作或需要少于100兆字节内存的非索引(“阻塞”)排序操作没有影响。For more complete documentation on blocking sorts and sort index use, see Sort and Index Use.有关阻塞排序和排序索引使用的更完整文档,请参阅排序和索引使用。
To check if MongoDB must perform an blocking 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 blocking 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 includes 该操作还包括支持排序操作的cursor.allowDiskUse()
to support the sort operation.cursor.allowDiskUse()
。
db.sensors.find({"sensor-location" : "Unit 12"}).
sort({"reading.timestamp" : 1}).
allowDiskUse()
Since 由于reading.timestamp
is not included in an index, MongoDB must perform a blocking sort operation to return results in the requested sort order. reading.timestamp
不包含在索引中,MongoDB必须执行阻塞排序操作才能按请求的排序顺序返回结果。By specifying 通过指定allowDiskUse()
, MongoDB can process the sort operation even if it requires more than 100 megabytes of system memory. allowDiskUse()
,MongoDB可以处理排序操作,即使它需要超过100兆字节的系统内存。If 如果省略了allowDiskUse()
was omitted and the operation required more than 100 megabytes of system memory, MongoDB would return an error.allowDiskUse()
,并且该操作需要超过100兆字节的系统内存,MongoDB将返回错误。