Query.prototype.cursor()

Parameters
  • [options] «Object»
Returns:
  • «QueryCursor»

Returns a wrapper around a mongodb driver cursor. A QueryCursor exposes a Streams3 interface, as well as a .next() function.

The .cursor() function triggers pre find hooks, but not post find hooks.

Example

  1. // There are 2 ways to use a cursor. First, as a stream:
  2. Thing.
  3. find({ name: /^hello/ }).
  4. cursor().
  5. on('data', function(doc) { console.log(doc); }).
  6. on('end', function() { console.log('Done!'); });
  7. // Or you can use `.next()` to manually get the next doc in the stream.
  8. // `.next()` returns a promise, so you can use promises or callbacks.
  9. const cursor = Thing.find({ name: /^hello/ }).cursor();
  10. cursor.next(function(error, doc) {
  11. console.log(doc);
  12. });
  13. // Because `.next()` returns a promise, you can use co
  14. // to easily iterate through all documents without loading them
  15. // all into memory.
  16. co(function*() {
  17. const cursor = Thing.find({ name: /^hello/ }).cursor();
  18. for (let doc = yield cursor.next(); doc != null; doc = yield cursor.next()) {
  19. console.log(doc);
  20. }
  21. });

Valid options

  • transform: optional function which accepts a mongoose document. The return value of the function will be emitted on data and returned by .next().