From e5948b8711201fd4db2a9e199c53a4fd06deb9d7 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Mon, 4 Feb 2019 19:37:29 -0500 Subject: [PATCH] fix(document): copy atomics when setting document array to an existing document array Fix #7472 --- lib/types/documentarray.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/types/documentarray.js b/lib/types/documentarray.js index 1899980715c..89ca8c6421e 100644 --- a/lib/types/documentarray.js +++ b/lib/types/documentarray.js @@ -44,12 +44,6 @@ function MongooseDocumentArray(values, path, doc) { // TODO: replace this with `new CoreMongooseArray().concat()` when we remove // support for node 4.x and 5.x, see https://i.imgur.com/UAAHk4S.png const arr = new CoreMongooseArray(); - if (Array.isArray(values)) { - values.forEach(v => { - arr.push(v); - }); - } - arr._path = path; const props = { isMongooseDocumentArray: true, @@ -59,6 +53,16 @@ function MongooseDocumentArray(values, path, doc) { _handlers: void 0 }; + if (Array.isArray(values)) { + if (values instanceof CoreMongooseArray) { + props._atomics = Object.assign({}, values._atomics); + } + values.forEach(v => { + arr.push(v); + }); + } + arr._path = path; + // Values always have to be passed to the constructor to initialize, since // otherwise MongooseArray#push will mark the array as modified to the parent. const keysMA = Object.keys(MongooseArray.mixin);