Skip to content

Commit 92cb6fb

Browse files
committed
Merge branch 'master' into vkarpov15/gh-12085
2 parents a45cfb6 + 422f9da commit 92cb6fb

File tree

6 files changed

+51
-24
lines changed

6 files changed

+51
-24
lines changed

docs/migrating_to_6.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,8 @@ mongoose.isValidObjectId(new User({ name: 'test' })); // true
194194
// character hex strings.
195195
mongoose.isObjectIdOrHexString(new mongoose.Types.ObjectId()); // true
196196
mongoose.isObjectIdOrHexString('62261a65d66c6be0a63c051f'); // true
197-
mongoose.isValidObjectId('0123456789ab'); // false
198-
mongoose.isValidObjectId(6); // false
197+
mongoose.isObjectIdOrHexString('0123456789ab'); // false
198+
mongoose.isObjectIdOrHexString(6); // false
199199
```
200200

201201
<h3 id="schema-defined-document-key-order"><a href="#schema-defined-document-key-order">Schema Defined Document Key Order</a></h3>

lib/document.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1149,8 +1149,8 @@ Document.prototype.$set = function $set(path, val, type, options) {
11491149
}
11501150

11511151
if (utils.isNonBuiltinObject(valForKey) && pathtype === 'nested') {
1152-
$applyDefaultsToNested(path[key], prefix + key, this);
11531152
this.$set(prefix + key, path[key], constructing, Object.assign({}, options, { _skipMarkModified: true }));
1153+
$applyDefaultsToNested(this.$get(prefix + key), prefix + key, this);
11541154
continue;
11551155
} else if (strict) {
11561156
// Don't overwrite defaults with undefined keys (gh-3981) (gh-9039)

lib/query.js

Lines changed: 8 additions & 2 deletions
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/document.test.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8831,7 +8831,7 @@ describe('document', function() {
88318831
assert.ok(!user.updatedAt);
88328832
});
88338833

8834-
it('Sets default when passing undefined as value for a key in a nested subdoc (gh-9039)', async function() {
8834+
it('Sets default when passing undefined as value for a key in a nested subdoc (gh-12102) (gh-9039)', async function() {
88358835
const Test = db.model('Test', {
88368836
nested: {
88378837
prop: {
@@ -8841,9 +8841,11 @@ describe('document', function() {
88418841
}
88428842
});
88438843

8844-
8845-
const doc = await Test.create({ nested: { prop: undefined } });
8844+
const obj = { nested: { prop: undefined } };
8845+
const doc = await Test.create(obj);
88468846
assert.equal(doc.nested.prop, 'some default value');
8847+
8848+
assert.deepStrictEqual(obj, { nested: { prop: undefined } });
88478849
});
88488850

88498851
it('allows accessing $locals when initializing (gh-9098)', function() {

test/query.test.js

Lines changed: 22 additions & 16 deletions
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() {

test/schema.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,19 @@ describe('schema', function() {
924924

925925
assert.equal(called, true);
926926
});
927+
928+
it('options param (gh-12077)', function() {
929+
const Tobi = new Schema();
930+
let called = false;
931+
932+
Tobi.plugin(function(schema, opts) {
933+
assert.equal(schema, Tobi);
934+
assert.deepStrictEqual(opts, { answer: 42 });
935+
called = true;
936+
}, { answer: 42 });
937+
938+
assert.equal(called, true);
939+
});
927940
});
928941

929942
describe('options', function() {

0 commit comments

Comments
 (0)