Skip to content

Commit 4b6d208

Browse files
authoredJan 18, 2024
Merge pull request #14254 from Automattic/vkarpov15/gh-14244
fix(document): allow calling `push()` with different `$position` arguments
2 parents cc9ac77 + 6bc42ce commit 4b6d208

File tree

2 files changed

+16
-17
lines changed

2 files changed

+16
-17
lines changed
 

‎lib/types/array/methods/index.js

+8-10
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
const Document = require('../../../document');
44
const ArraySubdocument = require('../../ArraySubdocument');
5-
const MongooseError = require('../../../error/mongooseError');
65
const cleanModifiedSubpaths = require('../../../helpers/document/cleanModifiedSubpaths');
76
const internalToObjectOptions = require('../../../options').internalToObjectOptions;
87
const mpath = require('mpath');
@@ -684,22 +683,21 @@ const methods = {
684683

685684
if ((atomics.$push && atomics.$push.$each && atomics.$push.$each.length || 0) !== 0 &&
686685
atomics.$push.$position != atomic.$position) {
687-
throw new MongooseError('Cannot call `Array#push()` multiple times ' +
688-
'with different `$position`');
689-
}
686+
if (atomic.$position != null) {
687+
[].splice.apply(arr, [atomic.$position, 0].concat(values));
688+
ret = arr.length;
689+
} else {
690+
ret = [].push.apply(arr, values);
691+
}
690692

691-
if (atomic.$position != null) {
693+
this._registerAtomic('$set', this);
694+
} else if (atomic.$position != null) {
692695
[].splice.apply(arr, [atomic.$position, 0].concat(values));
693696
ret = this.length;
694697
} else {
695698
ret = [].push.apply(arr, values);
696699
}
697700
} else {
698-
if ((atomics.$push && atomics.$push.$each && atomics.$push.$each.length || 0) !== 0 &&
699-
atomics.$push.$position != null) {
700-
throw new MongooseError('Cannot call `Array#push()` multiple times ' +
701-
'with different `$position`');
702-
}
703701
atomic = values;
704702
ret = [].push.apply(arr, values);
705703
}

‎test/document.test.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -8208,7 +8208,7 @@ describe('document', function() {
82088208
assert.deepEqual(Object.keys(err.errors), ['age']);
82098209
});
82108210

8211-
it('array push with $position (gh-4322)', async function() {
8211+
it('array push with $position (gh-14244) (gh-4322)', async function() {
82128212
const schema = Schema({
82138213
nums: [Number]
82148214
});
@@ -8232,12 +8232,13 @@ describe('document', function() {
82328232
$each: [0],
82338233
$position: 0
82348234
});
8235-
assert.throws(() => {
8236-
doc.nums.push({ $each: [5] });
8237-
}, /Cannot call.*multiple times/);
8238-
assert.throws(() => {
8239-
doc.nums.push(5);
8240-
}, /Cannot call.*multiple times/);
8235+
assert.deepStrictEqual(doc.nums.$__getAtomics(), [['$push', { $each: [0], $position: 0 }]]);
8236+
8237+
doc.nums.push({ $each: [5] });
8238+
assert.deepStrictEqual(doc.nums.$__getAtomics(), [['$set', [0, 1, 2, 3, 4, 5]]]);
8239+
8240+
doc.nums.push({ $each: [0.5], $position: 1 });
8241+
assert.deepStrictEqual(doc.nums.$__getAtomics(), [['$set', [0, 0.5, 1, 2, 3, 4, 5]]]);
82418242
});
82428243

82438244
it('setting a path to a single nested document should update the single nested doc parent (gh-8400)', function() {

0 commit comments

Comments
 (0)
Please sign in to comment.