Skip to content

Commit 704f30a

Browse files
committed
fix(cursor): transforms should only be applied once to documents
A regression in merging the core cursor into the native codebase resultied in a document transform (set with the `.map` helper) to be applied to incoming documents twice. NODE-2503
1 parent d7ac176 commit 704f30a

File tree

3 files changed

+55
-15
lines changed

3 files changed

+55
-15
lines changed

lib/core/cursor.js

-9
Original file line numberDiff line numberDiff line change
@@ -359,15 +359,6 @@ class CoreCursor extends Readable {
359359
return this.push(this.cursorState.streamOptions.transform(result));
360360
}
361361

362-
// If we provided a map function
363-
if (
364-
this.cursorState.transforms &&
365-
typeof this.cursorState.transforms.doc === 'function' &&
366-
result != null
367-
) {
368-
return this.push(this.cursorState.transforms.doc(result));
369-
}
370-
371362
// Return the result
372363
this.push(result);
373364

lib/cursor.js

+1-6
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,7 @@ class Cursor extends CoreCursor {
819819
driver: true
820820
});
821821
}
822+
822823
return maybePromise(this, callback, cb => {
823824
const cursor = this;
824825
const items = [];
@@ -846,12 +847,6 @@ class Cursor extends CoreCursor {
846847
// Get all buffered objects
847848
if (cursor.bufferedCount() > 0) {
848849
let docs = cursor.readBufferedDocuments(cursor.bufferedCount());
849-
850-
// Transform the doc if transform method added
851-
if (cursor.s.transforms && typeof cursor.s.transforms.doc === 'function') {
852-
docs = docs.map(cursor.s.transforms.doc);
853-
}
854-
855850
Array.prototype.push.apply(items, docs);
856851
}
857852

test/functional/cursor.test.js

+54
Original file line numberDiff line numberDiff line change
@@ -4609,4 +4609,58 @@ describe('Cursor', function() {
46094609
});
46104610
});
46114611
});
4612+
4613+
describe('transforms', function() {
4614+
it('should correctly apply map transform to cursor as readable stream', function(done) {
4615+
const configuration = this.configuration;
4616+
const client = configuration.newClient();
4617+
client.connect(err => {
4618+
expect(err).to.not.exist;
4619+
this.defer(() => client.close());
4620+
4621+
const docs = 'Aaden Aaron Adrian Aditya Bob Joe'.split(' ').map(x => ({ name: x }));
4622+
const coll = client.db(configuration.db).collection('cursor_stream_mapping');
4623+
coll.insertMany(docs, err => {
4624+
expect(err).to.not.exist;
4625+
4626+
const bag = [];
4627+
const stream = coll
4628+
.find()
4629+
.project({ _id: 0, name: 1 })
4630+
.map(doc => ({ mapped: doc }))
4631+
.on('data', doc => bag.push(doc));
4632+
4633+
stream.on('error', done).on('end', () => {
4634+
expect(bag.map(x => x.mapped)).to.eql(docs.map(x => ({ name: x.name })));
4635+
done();
4636+
});
4637+
});
4638+
});
4639+
});
4640+
4641+
it('should correctly apply map transform when converting cursor to array', function(done) {
4642+
const configuration = this.configuration;
4643+
const client = configuration.newClient();
4644+
client.connect(err => {
4645+
expect(err).to.not.exist;
4646+
this.defer(() => client.close());
4647+
4648+
const docs = 'Aaden Aaron Adrian Aditya Bob Joe'.split(' ').map(x => ({ name: x }));
4649+
const coll = client.db(configuration.db).collection('cursor_toArray_mapping');
4650+
coll.insertMany(docs, err => {
4651+
expect(err).to.not.exist;
4652+
4653+
coll
4654+
.find()
4655+
.project({ _id: 0, name: 1 })
4656+
.map(doc => ({ mapped: doc }))
4657+
.toArray((err, mappedDocs) => {
4658+
expect(err).to.not.exist;
4659+
expect(mappedDocs.map(x => x.mapped)).to.eql(docs.map(x => ({ name: x.name })));
4660+
done();
4661+
});
4662+
});
4663+
});
4664+
});
4665+
});
46124666
});

0 commit comments

Comments
 (0)