节点Mongo本机 - 如何告诉何时光标耗尽? [英] Node Mongo Native - how to tell when a cursor is exhausted?
问题描述
节点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 methodseach
andtoArray
callnextObject
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屋!