Skip to content

Commit 086bd9f

Browse files
committedJul 19, 2022
fix(query): apply lean transform option to top-level document
Fix #12093
1 parent bc302f4 commit 086bd9f

File tree

2 files changed

+30
-18
lines changed

2 files changed

+30
-18
lines changed
 

‎lib/query.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -4019,7 +4019,9 @@ Query.prototype._findAndModify = function(type, callback) {
40194019
*/
40204020

40214021
function _completeOneLean(schema, doc, path, res, opts, callback) {
4022-
if (opts.lean && opts.lean.transform) {
4022+
if (opts.lean && typeof opts.lean.transform === 'function') {
4023+
opts.lean.transform(doc);
4024+
40234025
for (let i = 0; i < schema.childSchemas.length; i++) {
40244026
const childPath = path ? path + '.' + schema.childSchemas[i].model.path : schema.childSchemas[i].model.path;
40254027
const _schema = schema.childSchemas[i].schema;
@@ -4053,7 +4055,11 @@ function _completeOneLean(schema, doc, path, res, opts, callback) {
40534055
*/
40544056

40554057
function _completeManyLean(schema, docs, path, opts, callback) {
4056-
if (opts.lean && opts.lean.transform) {
4058+
if (opts.lean && typeof opts.lean.transform === 'function') {
4059+
for (const doc of docs) {
4060+
opts.lean.transform(doc);
4061+
}
4062+
40574063
for (let i = 0; i < schema.childSchemas.length; i++) {
40584064
const childPath = path ? path + '.' + schema.childSchemas[i].model.path : schema.childSchemas[i].model.path;
40594065
const _schema = schema.childSchemas[i].schema;

‎test/query.test.js

+22-16
Original file line numberDiff line numberDiff line change
@@ -4006,22 +4006,28 @@ describe('Query', function() {
40064006
});
40074007
const Test = db.model('gh10423', testSchema);
40084008
await Test.create({ name: 'foo', foo: [{ sub: 'Test' }, { sub: 'Testerson' }], otherName: { nickName: 'Bar' } });
4009-
const result = await Test.find().lean({ transform: (doc) => {
4010-
delete doc._id;
4011-
return doc;
4012-
} });
4013-
assert(result[0]._id);
4014-
assert.equal(result[0].otherName._id, undefined);
4015-
assert.equal(result[0].foo[0]._id, undefined);
4016-
assert.equal(result[0].foo[1]._id, undefined);
4017-
const single = await Test.findOne().lean({ transform: (doc) => {
4018-
delete doc._id;
4019-
return doc;
4020-
} });
4021-
assert(single._id);
4022-
assert.equal(single.otherName._id, undefined);
4023-
assert.equal(single.foo[0]._id, undefined);
4024-
assert.equal(single.foo[0]._id, undefined);
4009+
4010+
const result = await Test.find().lean({
4011+
transform: (doc) => {
4012+
delete doc._id;
4013+
return doc;
4014+
}
4015+
});
4016+
assert.strictEqual(result[0]._id, undefined);
4017+
assert.strictEqual(result[0].otherName._id, undefined);
4018+
assert.strictEqual(result[0].foo[0]._id, undefined);
4019+
assert.strictEqual(result[0].foo[1]._id, undefined);
4020+
4021+
const single = await Test.findOne().lean({
4022+
transform: (doc) => {
4023+
delete doc._id;
4024+
return doc;
4025+
}
4026+
});
4027+
assert.strictEqual(single._id, undefined);
4028+
assert.strictEqual(single.otherName._id, undefined);
4029+
assert.strictEqual(single.foo[0]._id, undefined);
4030+
assert.strictEqual(single.foo[0]._id, undefined);
40254031
});
40264032

40274033
it('skips applying default projections over slice projections (gh-11940)', async function() {

0 commit comments

Comments
 (0)
Please sign in to comment.