Database Manual / Reference / mongosh Methods / Cursors

cursor.isExhausted() (mongosh method方法)

cursor.isExhausted()

Important

mongosh Method方法

This page documents a mongosh method. 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驱动程序文档

Returns:返回Boolean布尔值

cursor.isExhausted() returns false if documents remain in the current document batch read by the cursor. Otherwise, returns true.如果文档仍在游标读取的当前文档批中,则cursor.isExhausted()返回false。否则,返回true

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

Tailable Cursors可定制的游标

You can use isExhausted() with a tailable cursor. A tailable cursor stays open even if no documents remain in the current batch. Other cursors are automatically closed when no documents remain.您可以将isExhausted()与可尾随游标一起使用。即使当前批次中没有文档,可尾随游标也会保持打开状态。当没有文档时,其他游标会自动关闭。

Change Streams更改流

You cannot use isExhausted() with change streams. Instead, to examine if:您不能将isExhausted()用于更改流。相反,检查是否:

For change stream examples, see Watch Example and Change Stream Images Example.有关更改流示例,请参阅监视示例更改流图像示例

Examples示例

This section contains examples that use a cursor to read documents from a collection with temperature readings from a weather sensor. You'll see examples of isExhausted().本节包含使用游标从包含天气传感器温度读数的集合中读取文档的示例。您将看到isExhausted()的示例。

1

Create the collection创建集合

Run:运行:

db.sensor.insertMany( [
{ _id: 0, temperature: 12 },
{ _id: 1, temperature: 23 }
] )
2

Create a cursor variable创建游标变量

Create a cursor variable named sensorCursor that reads the documents from the sensor collection:创建一个名为sensorCursor的游标变量,用于从sensor集合中读取文档:

var sensorCursor = db.sensor.find()
3

Count the number of documents in the cursor统计游标中的文档数量

Run:运行:

sensorCursor.count()

The output is 2 because there are two documents in the collection.输出为2,因为集合中有两个文档。

4

Return the first document from the cursor返回游标中的第一个文档

Run:运行:

sensorCursor.next()

Output:输出:

{ _id: 0, temperature: 12 }
5

Examine the isExhausted() value检查isExhausted()

Run:运行:

sensorCursor.isExhausted()

The output is false because there is a remaining document in sensorCursor.输出为false,因为sensorCursor中还有剩余文档。

6

Return the next document from the cursor从游标返回下一个文档

Run:运行:

sensorCursor.next()

Output:输出:

{ _id: 1, temperature: 23 }
7

Attempt to return the next document from the cursor尝试从游标返回下一个文档

Run:运行:

sensorCursor.next()

There are no more documents and the example returns null.没有更多文档,示例返回null

8

Examine the isExhausted() value检查isExhausted()

Run:运行:

sensorCursor.isExhausted()

There are no more documents and isExhausted() returns true.没有更多文档,isExhausted()返回true