On this page本页内容
cursor.isExhausted()
This is a mongosh
method. This is not the documentation for Node.js
or other programming language specific driver methods.
In most cases, mongosh
methods work the same way as the legacy mongo
shell methods. However, some legacy methods are unavailable in mongosh
.
For the legacy mongo
shell documentation, refer to the documentation for the corresponding MongoDB Server release:
For MongoDB API drivers, refer to the language specific MongoDB driver documentation.
Boolean. |
如果游标关闭且批处理中没有剩余对象,cursor.isExhausted()
returns true
if the cursor is closed and there are no remaining objects in the batch.cursor.isExhausted()
将返回true
。
Use 使用isExhausted()
to support iterating cursors that remain open even if there are no documents remaining in the current batch, such as tailable
or change stream
cursors.isExhausted()
支持迭代游标,即使当前批处理中没有剩余文档,这些游标仍保持打开状态,例如tailable
游标或更改流游标。
Consider the following 考虑以下while
loop iterating through updates to a change stream
cursor:while
循环迭代更改流游标的更新:
watchCursor = db.collection.watch();
while (watchCursor.hasNext()) {
watchCursor.next();
}
A change stream cursor can return an empty batch if no new data changes have occured within a set period of time. 如果在设定的时间段内没有发生新的数据更改,则更改流游标可以返回空批次。This causes the while loop to exit prematurely as 这会导致cursor.hasNext()
returns false
when it detects the empty batch. while
循环过早退出,因为cursor.hasNext()
在检测到空批时返回false
。However, the change stream cursor is still open and able to return more documents in the future.然而,变更流游标仍处于打开状态,将来可以返回更多文档。
Use 使用cursor.isExhausted()
to ensure the while loop only exits when the cursor is closed and there are no documents remaining in the batch:cursor.isExhausted()
确保while
循环仅在游标关闭且批处理中没有剩余文档时存在:
watchCursor = db.collection.watch(); while (!watchCursor.isExhausted()) { if (watchCursor.hasNext()){ watchCursor.next(); } }