Skip to content

Commit

Permalink
fix(query): apply lean transform option to top-level document
Browse files Browse the repository at this point in the history
Fix #12093
  • Loading branch information
vkarpov15 committed Jul 19, 2022
1 parent bc302f4 commit 086bd9f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 18 deletions.
10 changes: 8 additions & 2 deletions lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -4019,7 +4019,9 @@ Query.prototype._findAndModify = function(type, callback) {
*/

function _completeOneLean(schema, doc, path, res, opts, callback) {
if (opts.lean && opts.lean.transform) {
if (opts.lean && typeof opts.lean.transform === 'function') {
opts.lean.transform(doc);

for (let i = 0; i < schema.childSchemas.length; i++) {
const childPath = path ? path + '.' + schema.childSchemas[i].model.path : schema.childSchemas[i].model.path;
const _schema = schema.childSchemas[i].schema;
Expand Down Expand Up @@ -4053,7 +4055,11 @@ function _completeOneLean(schema, doc, path, res, opts, callback) {
*/

function _completeManyLean(schema, docs, path, opts, callback) {
if (opts.lean && opts.lean.transform) {
if (opts.lean && typeof opts.lean.transform === 'function') {
for (const doc of docs) {
opts.lean.transform(doc);
}

for (let i = 0; i < schema.childSchemas.length; i++) {
const childPath = path ? path + '.' + schema.childSchemas[i].model.path : schema.childSchemas[i].model.path;
const _schema = schema.childSchemas[i].schema;
Expand Down
38 changes: 22 additions & 16 deletions test/query.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4006,22 +4006,28 @@ describe('Query', function() {
});
const Test = db.model('gh10423', testSchema);
await Test.create({ name: 'foo', foo: [{ sub: 'Test' }, { sub: 'Testerson' }], otherName: { nickName: 'Bar' } });
const result = await Test.find().lean({ transform: (doc) => {
delete doc._id;
return doc;
} });
assert(result[0]._id);
assert.equal(result[0].otherName._id, undefined);
assert.equal(result[0].foo[0]._id, undefined);
assert.equal(result[0].foo[1]._id, undefined);
const single = await Test.findOne().lean({ transform: (doc) => {
delete doc._id;
return doc;
} });
assert(single._id);
assert.equal(single.otherName._id, undefined);
assert.equal(single.foo[0]._id, undefined);
assert.equal(single.foo[0]._id, undefined);

const result = await Test.find().lean({
transform: (doc) => {
delete doc._id;
return doc;
}
});
assert.strictEqual(result[0]._id, undefined);
assert.strictEqual(result[0].otherName._id, undefined);
assert.strictEqual(result[0].foo[0]._id, undefined);
assert.strictEqual(result[0].foo[1]._id, undefined);

const single = await Test.findOne().lean({
transform: (doc) => {
delete doc._id;
return doc;
}
});
assert.strictEqual(single._id, undefined);
assert.strictEqual(single.otherName._id, undefined);
assert.strictEqual(single.foo[0]._id, undefined);
assert.strictEqual(single.foo[0]._id, undefined);
});

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

0 comments on commit 086bd9f

Please sign in to comment.