diff --git a/index.js b/index.js index efcf7ad..c7eba56 100644 --- a/index.js +++ b/index.js @@ -88,6 +88,13 @@ Cursor.prototype.destroy = function() { this.push(null); }; +Cursor.prototype.map = function(mapfn, callback) { + this.toArray(function(err, arr) { + if (err) return callback(err); + callback(null, arr.map(mapfn)) + }); +}; + Cursor.prototype._apply = function(fn, args) { this._get(function(err, cursor) { if (err) return getCallback(args)(err); diff --git a/tests/test-cursor-map.js b/tests/test-cursor-map.js new file mode 100644 index 0000000..06dfce3 --- /dev/null +++ b/tests/test-cursor-map.js @@ -0,0 +1,18 @@ +var assert = require('assert'); +var insert = require('./insert'); + +insert([{ + hello:'world1' +},{ + hello:'world2' +}], function(db, done) { + var cursor = db.a.find(); + cursor.map(function(x) { + return x.hello + }, function(err, res) { + assert.equal(res[0], 'world1'); + assert.equal(res[1], 'world2'); + done(); + }); +}); +