Docs HomeMongoDB Manual

cursor.isExhausted()

On this page本页内容

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.

For MongoDB API drivers, refer to the language-specific MongoDB driver documentation.

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

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