From 1344214cc152159f7788fd3b2a013590e1c0c679 Mon Sep 17 00:00:00 2001 From: Nagalokesh <20608938+LokeshKanumoori@users.noreply.github.com> Date: Tue, 19 Jul 2022 19:11:43 +0530 Subject: [PATCH 1/4] Update migrating_to_6.md correct the proof/example statement of isObjectIdOrHexString --- docs/migrating_to_6.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/migrating_to_6.md b/docs/migrating_to_6.md index 270624e3368..68e4a8249e0 100644 --- a/docs/migrating_to_6.md +++ b/docs/migrating_to_6.md @@ -194,8 +194,8 @@ mongoose.isValidObjectId(new User({ name: 'test' })); // true // character hex strings. mongoose.isObjectIdOrHexString(new mongoose.Types.ObjectId()); // true mongoose.isObjectIdOrHexString('62261a65d66c6be0a63c051f'); // true -mongoose.isValidObjectId('0123456789ab'); // false -mongoose.isValidObjectId(6); // false +mongoose.isObjectIdOrHexString('0123456789ab'); // false +mongoose.isObjectIdOrHexString(6); // false ```

Schema Defined Document Key Order

From 086bd9f3f9aa06396a922d9948eee0248cf4bbb8 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Tue, 19 Jul 2022 11:54:05 -0400 Subject: [PATCH 2/4] fix(query): apply lean transform option to top-level document Fix #12093 --- lib/query.js | 10 ++++++++-- test/query.test.js | 38 ++++++++++++++++++++++---------------- 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/lib/query.js b/lib/query.js index 1bfed85a27e..634616fe79a 100644 --- a/lib/query.js +++ b/lib/query.js @@ -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; @@ -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; diff --git a/test/query.test.js b/test/query.test.js index 746774c5c42..1ebc5320aa4 100644 --- a/test/query.test.js +++ b/test/query.test.js @@ -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() { From 2262a7709fe72ea255d23e1aea4ac84e9627e523 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Tue, 19 Jul 2022 20:16:53 -0400 Subject: [PATCH 3/4] fix(document): avoid mutating original object passed to $set() when applying defaults to nested properties Fix #12102 --- lib/document.js | 2 +- test/document.test.js | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/document.js b/lib/document.js index 72c12c75442..e6f73aeb774 100644 --- a/lib/document.js +++ b/lib/document.js @@ -1149,8 +1149,8 @@ Document.prototype.$set = function $set(path, val, type, options) { } if (utils.isNonBuiltinObject(valForKey) && pathtype === 'nested') { - $applyDefaultsToNested(path[key], prefix + key, this); this.$set(prefix + key, path[key], constructing, Object.assign({}, options, { _skipMarkModified: true })); + $applyDefaultsToNested(this.$get(prefix + key), prefix + key, this); continue; } else if (strict) { // Don't overwrite defaults with undefined keys (gh-3981) (gh-9039) diff --git a/test/document.test.js b/test/document.test.js index 160b14fbe3d..359a0ee050d 100644 --- a/test/document.test.js +++ b/test/document.test.js @@ -8831,7 +8831,7 @@ describe('document', function() { assert.ok(!user.updatedAt); }); - it('Sets default when passing undefined as value for a key in a nested subdoc (gh-9039)', async function() { + it('Sets default when passing undefined as value for a key in a nested subdoc (gh-12102) (gh-9039)', async function() { const Test = db.model('Test', { nested: { prop: { @@ -8841,9 +8841,11 @@ describe('document', function() { } }); - - const doc = await Test.create({ nested: { prop: undefined } }); + const obj = { nested: { prop: undefined } }; + const doc = await Test.create(obj); assert.equal(doc.nested.prop, 'some default value'); + + assert.deepStrictEqual(obj, { nested: { prop: undefined } }); }); it('allows accessing $locals when initializing (gh-9098)', function() { From 422f9da02d2e8c0c227887265bce25cecaecf403 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Tue, 19 Jul 2022 20:23:54 -0400 Subject: [PATCH 4/4] test(schema): add coverage for calling `plugin()` with options Fix #12077 --- test/schema.test.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/schema.test.js b/test/schema.test.js index 7bc6299e23e..8cfac9618a2 100644 --- a/test/schema.test.js +++ b/test/schema.test.js @@ -924,6 +924,19 @@ describe('schema', function() { assert.equal(called, true); }); + + it('options param (gh-12077)', function() { + const Tobi = new Schema(); + let called = false; + + Tobi.plugin(function(schema, opts) { + assert.equal(schema, Tobi); + assert.deepStrictEqual(opts, { answer: 42 }); + called = true; + }, { answer: 42 }); + + assert.equal(called, true); + }); }); describe('options', function() {