cursor.allowDiskUse()

On this page本页内容

Definition定义

New in version 4.4.版本4.4中的新功能。

cursor.allowDiskUse()

mongo Shell Method

This page documents the mongo shell method, and does not refer to the MongoDB Node.js driver (or any other driver) method. 本页记录了mongo shell方法,未提及MongoDB Node.js驱动程序(或任何其他驱动程序)方法。For corresponding MongoDB driver API, refer to your specific MongoDB driver documentation instead.有关相应的MongoDB驱动程序API,请参阅特定的MongoDB驱动程序文档。

allowDiskUse() allows MongoDB to use temporary files on disk to store data exceeding the 100 megabyte system memory limit while processing a blocking sort operation. If MongoDB requires using more than 100 megabytes of system memory for the blocking sort operation, MongoDB returns an error unless the query specifies cursor.allowDiskUse().

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行为

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. 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 cursor.explain() to the query and check the explain results. If the query plan contains a SORT stage, then MongoDB must perform an blocking sort operation subject to the 100 megabyte memory limit.

Example示例

Consider a collection sensors with only the default index on _id. 考虑一个只有在默认_id上的集合sensorsThe collection contains documents similar to the following:该集合包含与以下内容类似的文档:

{
  "sensor-name" : "TEMP-21425",
  "sensor-location" : "Unit 12",
  "reading" : {
    "timestamp" : Timestamp(1580247215, 1),
    "value" : 212,
    "unit" : "Farenheit"
  }
}

The following operation includes a cursor.sort() on the field reading.timestamp. The operation also includes cursor.allowDiskUse() to support the sort operation.

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. By specifying allowDiskUse(), MongoDB can process the sort operation even if it requires more than 100 megabytes of system memory. If allowDiskUse() was omitted and the operation required more than 100 megabytes of system memory, MongoDB would return an error.