diff --git a/lib/core/cursor.js b/lib/core/cursor.js index 8b6aa3dfc7..72d309eaf9 100644 --- a/lib/core/cursor.js +++ b/lib/core/cursor.js @@ -359,15 +359,6 @@ class CoreCursor extends Readable { return this.push(this.cursorState.streamOptions.transform(result)); } - // If we provided a map function - if ( - this.cursorState.transforms && - typeof this.cursorState.transforms.doc === 'function' && - result != null - ) { - return this.push(this.cursorState.transforms.doc(result)); - } - // Return the result this.push(result); diff --git a/lib/cursor.js b/lib/cursor.js index 1a39a2eeef..0e8d062337 100644 --- a/lib/cursor.js +++ b/lib/cursor.js @@ -819,6 +819,7 @@ class Cursor extends CoreCursor { driver: true }); } + return maybePromise(this, callback, cb => { const cursor = this; const items = []; @@ -846,12 +847,6 @@ class Cursor extends CoreCursor { // Get all buffered objects if (cursor.bufferedCount() > 0) { let docs = cursor.readBufferedDocuments(cursor.bufferedCount()); - - // Transform the doc if transform method added - if (cursor.s.transforms && typeof cursor.s.transforms.doc === 'function') { - docs = docs.map(cursor.s.transforms.doc); - } - Array.prototype.push.apply(items, docs); } diff --git a/test/functional/cursor.test.js b/test/functional/cursor.test.js index f039e6f82e..eeddda3ada 100644 --- a/test/functional/cursor.test.js +++ b/test/functional/cursor.test.js @@ -4609,4 +4609,58 @@ describe('Cursor', function() { }); }); }); + + describe('transforms', function() { + it('should correctly apply map transform to cursor as readable stream', function(done) { + const configuration = this.configuration; + const client = configuration.newClient(); + client.connect(err => { + expect(err).to.not.exist; + this.defer(() => client.close()); + + const docs = 'Aaden Aaron Adrian Aditya Bob Joe'.split(' ').map(x => ({ name: x })); + const coll = client.db(configuration.db).collection('cursor_stream_mapping'); + coll.insertMany(docs, err => { + expect(err).to.not.exist; + + const bag = []; + const stream = coll + .find() + .project({ _id: 0, name: 1 }) + .map(doc => ({ mapped: doc })) + .on('data', doc => bag.push(doc)); + + stream.on('error', done).on('end', () => { + expect(bag.map(x => x.mapped)).to.eql(docs.map(x => ({ name: x.name }))); + done(); + }); + }); + }); + }); + + it('should correctly apply map transform when converting cursor to array', function(done) { + const configuration = this.configuration; + const client = configuration.newClient(); + client.connect(err => { + expect(err).to.not.exist; + this.defer(() => client.close()); + + const docs = 'Aaden Aaron Adrian Aditya Bob Joe'.split(' ').map(x => ({ name: x })); + const coll = client.db(configuration.db).collection('cursor_toArray_mapping'); + coll.insertMany(docs, err => { + expect(err).to.not.exist; + + coll + .find() + .project({ _id: 0, name: 1 }) + .map(doc => ({ mapped: doc })) + .toArray((err, mappedDocs) => { + expect(err).to.not.exist; + expect(mappedDocs.map(x => x.mapped)).to.eql(docs.map(x => ({ name: x.name }))); + done(); + }); + }); + }); + }); + }); });