Skip to content

Commit

Permalink
Merge pull request #1521 from Robbilie/patch-1
Browse files Browse the repository at this point in the history
Allow multiple map calls
  • Loading branch information
christkv authored Jun 12, 2017
2 parents 01b189d + 0e2610c commit 2fcf951
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/cursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -1003,7 +1003,12 @@ define.classMethod('close', {callback: true, promise:true});
* @return {Cursor}
*/
Cursor.prototype.map = function(transform) {
this.cursorState.transforms = { doc: transform };
if(this.cursorState.transforms && this.cursorState.transforms.doc) {
var oldTransform = this.cursorState.transforms.doc;
this.cursorState.transforms.doc = function (doc) { return transform(oldTransform(doc)); };
} else {
this.cursorState.transforms = { doc: transform };
}
return this;
}

Expand Down
47 changes: 47 additions & 0 deletions test/functional/cursor_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -3017,6 +3017,53 @@ exports['Should correctly apply map to forEach'] = {

var collection = db.collection('map_forEach');

// insert all docs
collection.insert(docs, configuration.writeConcernMax(), function(err, result) {
test.equal(null, err);
var total = 0;

// Create a cursor for the content
var cursor = collection.find({})
.map(function(x) { return {a:2}; })
.map(function(x) { return {a: x.a * x.a }; })
.batchSize(5)
.limit(10);
cursor.forEach(function(doc) {
test.equal(4, doc.a);
}, function(err, doc) {
test.equal(null, err);
db.close();
test.done();
});
})
});
}
}

/**
* @ignore
* @api private
*/
exports['Should correctly apply multiple uses of map and apply forEach'] = {
// Add a tag that our runner can trigger on
// in this case we are setting that node needs to be higher than 0.10.X to run
metadata: { requires: { topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] } },

// The actual test we wish to run
test: function(configuration, test) {
var docs = [];

for(var i = 0; i < 1000; i++) {
var d = new Date().getTime() + i*1000;
docs[i] = {'a':i, createdAt:new Date(d)};
}

var db = configuration.newDbInstance(configuration.writeConcernMax(), {poolSize:1});
db.open(function(err, db) {
test.equal(null, err);

var collection = db.collection('map_mapmapforEach');

// insert all docs
collection.insert(docs, configuration.writeConcernMax(), function(err, result) {
test.equal(null, err);
Expand Down

0 comments on commit 2fcf951

Please sign in to comment.