Node Mongo Native - how to say when the cursor is exhausted?

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

The main cursor operation is the nextObject method, which fetches the next matching document from the database. The convenience methods each and toArray call nextObject until the cursor is exhausted.

Unfortunately, the documentation does not indicate how to say when the cursor is actually exhausted. You can use the "toArray" method and use the standard array interface (for example, the "length" method), but this solution is not suitable for streaming large amounts of data. The MongoDB API refers to 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 while streaming data from MongoDB to node.js?

+4
mongodb node-mongodb-native cursor
source share
1 answer

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

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

+9
source share