AggregationCursor.js 759 B

12345678910111213141516171819202122232425
  1. var AggregationCursor = module.exports = function(pipelineInst) {
  2. this.pipelineInst = pipelineInst;
  3. }, klass = AggregationCursor, proto = klass.prototype;
  4. proto.toArray = function(callback) {
  5. var batch = [];
  6. this.pipelineInst.run(function(err, doc) {
  7. if (err && callback) return callback(err), callback = undefined;
  8. if (err && !callback) throw err;
  9. if (doc === null && callback) return callback(null, batch), callback = undefined;
  10. else if (doc !== null) batch.push(doc);
  11. });
  12. if (!callback) return batch;
  13. };
  14. proto.forEach = function(iterator, callback) {
  15. this.pipelineInst.run(function(err, doc) {
  16. if (err || doc === null) return callback(err);
  17. iterator(doc);
  18. });
  19. };
  20. proto.each = function(callback) {
  21. this.pipelineInst.run(callback);
  22. };