Skip to content

Allow multiple map calls #1521

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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