Docs 菜单
Docs 主页
/
MongoDB Manual
/ / /

cursor.isExhausted()

在此页面上

  • 行为
  • 举例
cursor.isExhausted()

重要

mongosh 方法

本页面提供 mongosh 方法的相关信息。这不是特定于语言的驱动程序(例如 Node.js)的文档。

如需了解 MongoDB API 驱动程序,请参阅特定语言的 MongoDB 驱动程序文档。

返回:布尔

如果文档仍在游标读取的当前文档批处理中, cursor.isExhausted()会返回false 。否则,返回true

您可以将isExhausted()可追加游标一起使用。即使当前批处理中没有剩余文档,可追加游标也会保持打开状态。当没有剩余文档时,其他游标会自动关闭。

您不能将isExhausted()change stream一起使用。相反,要检查是否:

  • 文档保留在change stream游标中,请使用cursor.tryNext()

  • change stream游标已关闭,请使用cursor.isClosed()

有关change stream示例,请参阅监视示例change stream图像示例。

This section contains examples that use a cursor to read documents from a collection with temperature readings from a weather sensor. 您将看到isExhausted()的示例。

1

运行:

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

创建一个名为sensorCursor的游标变量,用于读取sensor集合中的文档:

var sensorCursor = db.sensor.find()
3

运行:

sensorCursor.count()

输出为2 ,因为collection中有两个文档。

4

运行:

sensorCursor.next()

输出:

{ _id: 0, temperature: 12 }
5

运行:

sensorCursor.isExhausted()

输出为false ,因为sensorCursor中有剩余文档。

6

运行:

sensorCursor.next()

输出:

{ _id: 1, temperature: 23 }
7

运行:

sensorCursor.next()

没有更多文档,该示例返回null

8

运行:

sensorCursor.isExhausted()

没有更多文档, isExhausted()返回true

后退

cursor.hint

在此页面上