cursor.isExhausted()-
Important
mongosh
Method方法This page documents a本页记录了一种mongoshmethod. 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()returnsfalseif documents remain in the current document batch read by the cursor. Otherwise, returnstrue.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()用于更改流。相反,检查是否:
documents remain in a change stream cursor, use文档保留在更改流游标中,请使用cursor.tryNext().cursor.tryNext()。a change stream cursor is closed, use如果更改流游标已关闭,请使用cursor.isClosed().cursor.isClosed()。
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()的示例。
Create the collection创建集合
Run:运行:
db.sensor.insertMany( [
{ _id: 0, temperature: 12 },
{ _id: 1, temperature: 23 }
] )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()Count the number of documents in the cursor统计游标中的文档数量
Run:运行:
sensorCursor.count()
The output is 输出为2 because there are two documents in the collection.2,因为集合中有两个文档。
Return the first document from the cursor返回游标中的第一个文档
Run:运行:
sensorCursor.next()
Output:输出:
{ _id: 0, temperature: 12 }Examine the isExhausted() value检查isExhausted()值
isExhausted() valueRun:运行:
sensorCursor.isExhausted()
The output is 输出为false because there is a remaining document in sensorCursor.false,因为sensorCursor中还有剩余文档。
Return the next document from the cursor从游标返回下一个文档
Run:运行:
sensorCursor.next()
Output:输出:
{ _id: 1, temperature: 23 }