On this page
cursor.isExhausted()
在本页面
cursor.
isExhausted
( )
Returns: | Boolean. |
---|
如果光标关闭,并且批次中没有剩余的对象,则cursor.isExhausted()返回true
。
使用isExhausted()支持即使在当前批处理中没有剩余文档的情况下仍保持打开状态的迭代游标,例如tailable或change stream游标。
Example
考虑下面的while
循环迭代对change stream游标的更新:
watchCursor = db.collection.watch();
while (watchCursor.hasNext()) {
watchCursor.next();
}
如果在设定的时间内没有发生新的数据更改,则更改流游标可以返回空批处理。当cursor.hasNext()检测到空批处理时,这会导致 while 循环过早退出,因为cursor.hasNext()返回false
。但是,更改流游标仍处于打开状态,以后可以返回更多文档。
使用cursor.isExhausted()以确保 while 循环仅在关闭光标时退出,并且*批处理中没有剩余文档:
watchCursor = db.collection.watch();
while (!watchCursor.isExhausted()) {
if (watchCursor.hasNext()){
watchCursor.next();
}
}