节点Mongo本机 - 如何告诉何时光标耗尽? [英] Node Mongo Native - how to tell when a cursor is exhausted?

查看:160
本文介绍了节点Mongo本机 - 如何告诉何时光标耗尽?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

节点mongo-native collection.find的文档()函数说,它创建一个游标对象,它会延迟返回匹配的文档。此外:


游标的基本操作是 nextObject 方法,下一个匹配文档从数据库。方便的方法每个 toArray 调用 nextObject


不幸的是,文档没有提供如何判断光标实际用完的指示。您可以使用toArray方法并使用标准数组接口(例如length方法),但此解决方案不适合流式传输大量数据。 MongoDB API Wiki指的是 cursor.hasNext( ) ,但是这个方法在node.js驱动程序中似乎不可用。



/mongodb.github.com/node-mongodb-native/api-generated/cursor.html#nextobjectrel =nofollow noreferrer>文档 光标#nextObject 确定如果没有更多结果可用,它的回调的第二个参数是 null


第一个参数将包含错误对象,而第二个参数将包含来自返回结果的文档,如果没有更多结果,则为null。



The documentation for the node-mongo-native collection.find() function says that it creates a cursor object which lazily returns the matching documents. Furthermore:

The basic operation on a cursor is the nextObject method that fetches the next matching document from the database. The convenience methods each and toArray call nextObject until the cursor is exhausted.

Unfortunately, the documentation provides no indication of how to tell when the cursor is actually exhausted. You could use the "toArray" method and use the standard array interface (e.g. the "length" method) but this solution is inappropriate for streaming large amounts of data. The MongoDB API Wiki refers to the cursor.hasNext() in the mongo shell but this method does not seem to be available in the node.js driver.

How can you determine when the cursor is exhausted when streaming data from MongoDB in node.js?

解决方案

The documentation for Cursor#nextObject does define that the second parameter to its callback is null if there are no more results available.

The first parameter will contain an error object on error while the second parameter will contain a document from the returned result or null if there are no more results.

这篇关于节点Mongo本机 - 如何告诉何时光标耗尽?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆