Skip to content

Commit 2fcf951

Browse files
authored
Merge pull request #1521 from Robbilie/patch-1
Allow multiple map calls
2 parents 01b189d + 0e2610c commit 2fcf951

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

lib/cursor.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1003,7 +1003,12 @@ define.classMethod('close', {callback: true, promise:true});
10031003
* @return {Cursor}
10041004
*/
10051005
Cursor.prototype.map = function(transform) {
1006-
this.cursorState.transforms = { doc: transform };
1006+
if(this.cursorState.transforms && this.cursorState.transforms.doc) {
1007+
var oldTransform = this.cursorState.transforms.doc;
1008+
this.cursorState.transforms.doc = function (doc) { return transform(oldTransform(doc)); };
1009+
} else {
1010+
this.cursorState.transforms = { doc: transform };
1011+
}
10071012
return this;
10081013
}
10091014

test/functional/cursor_tests.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3017,6 +3017,53 @@ exports['Should correctly apply map to forEach'] = {
30173017

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

3020+
// insert all docs
3021+
collection.insert(docs, configuration.writeConcernMax(), function(err, result) {
3022+
test.equal(null, err);
3023+
var total = 0;
3024+
3025+
// Create a cursor for the content
3026+
var cursor = collection.find({})
3027+
.map(function(x) { return {a:2}; })
3028+
.map(function(x) { return {a: x.a * x.a }; })
3029+
.batchSize(5)
3030+
.limit(10);
3031+
cursor.forEach(function(doc) {
3032+
test.equal(4, doc.a);
3033+
}, function(err, doc) {
3034+
test.equal(null, err);
3035+
db.close();
3036+
test.done();
3037+
});
3038+
})
3039+
});
3040+
}
3041+
}
3042+
3043+
/**
3044+
* @ignore
3045+
* @api private
3046+
*/
3047+
exports['Should correctly apply multiple uses of map and apply forEach'] = {
3048+
// Add a tag that our runner can trigger on
3049+
// in this case we are setting that node needs to be higher than 0.10.X to run
3050+
metadata: { requires: { topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] } },
3051+
3052+
// The actual test we wish to run
3053+
test: function(configuration, test) {
3054+
var docs = [];
3055+
3056+
for(var i = 0; i < 1000; i++) {
3057+
var d = new Date().getTime() + i*1000;
3058+
docs[i] = {'a':i, createdAt:new Date(d)};
3059+
}
3060+
3061+
var db = configuration.newDbInstance(configuration.writeConcernMax(), {poolSize:1});
3062+
db.open(function(err, db) {
3063+
test.equal(null, err);
3064+
3065+
var collection = db.collection('map_mapmapforEach');
3066+
30203067
// insert all docs
30213068
collection.insert(docs, configuration.writeConcernMax(), function(err, result) {
30223069
test.equal(null, err);

0 commit comments

Comments
 (0)