Node Mongo Native - how to tell when a cursor is exhausted?

梦想与她 提交于 2019-12-12 09:42:28

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/12826520/node-mongo-native-how-to-tell-when-a-cursor-is-exhausted

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!