From 31ad004b64bff002cc8e3b918422ba9e157dc1d7 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Mon, 7 Sep 2020 16:37:24 -0400 Subject: [PATCH] fix(schema): support setting `_id` option to `false` after instantiating schema Fix #9390 --- lib/schema.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib/schema.js b/lib/schema.js index cbc1e80bd99..72410395747 100644 --- a/lib/schema.js +++ b/lib/schema.js @@ -1656,6 +1656,16 @@ Schema.prototype.set = function(key, value, _tags) { this.options[key] = value; this._userProvidedOptions[key] = this.options[key]; break; + case '_id': + this.options[key] = value; + this._userProvidedOptions[key] = this.options[key]; + + if (value && !this.paths['_id']) { + addAutoId(this); + } else if (!value && this.paths['_id'] != null && this.paths['_id'].auto) { + this.remove('_id'); + } + break; default: this.options[key] = value; this._userProvidedOptions[key] = this.options[key];