From 9a2462c81f6fe74d8e3a05a28f5e9fd7227ea48e Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Mon, 29 Jul 2024 13:54:25 -0400 Subject: [PATCH] fix: allow setting document array default to `null` Fix #6691 Re: #14717 --- lib/schema/documentArray.js | 2 +- test/document.test.js | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/schema/documentArray.js b/lib/schema/documentArray.js index a10d2ec76b1..aa0c0d7984a 100644 --- a/lib/schema/documentArray.js +++ b/lib/schema/documentArray.js @@ -69,7 +69,7 @@ function SchemaDocumentArray(key, schema, options, schemaOptions) { const fn = this.defaultValue; - if (!('defaultValue' in this) || fn !== void 0) { + if (!('defaultValue' in this) || fn != null) { this.default(function() { let arr = fn.call(this); if (arr != null && !Array.isArray(arr)) { diff --git a/test/document.test.js b/test/document.test.js index c11f72753ee..5957556a867 100644 --- a/test/document.test.js +++ b/test/document.test.js @@ -3203,16 +3203,22 @@ describe('document', function() { names: { type: [String], default: null + }, + tags: { + type: [{ tag: String }], + default: null } }); const Model = db.model('Test', schema); const m = new Model(); assert.strictEqual(m.names, null); + assert.strictEqual(m.tags, null); await m.save(); const doc = await Model.collection.findOne({ _id: m._id }); assert.strictEqual(doc.names, null); + assert.strictEqual(doc.tags, null); }); it('validation works when setting array index (gh-3816)', async function() {