Skip to content

Commit

Permalink
BREAKING CHANGE: use $push with $each instead of $pushAll, drop suppo…
Browse files Browse the repository at this point in the history
…rt for $pushAll

Fix #5670
  • Loading branch information
vkarpov15 committed Dec 5, 2017
1 parent b10d520 commit 82aeba5
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 181 deletions.
11 changes: 3 additions & 8 deletions lib/model.js
Expand Up @@ -381,7 +381,6 @@ function operand(self, where, delta, data, val, op) {
case '$pull':
case '$pullAll':
case '$push':
case '$pushAll':
case '$addToSet':
break;
default:
Expand All @@ -394,7 +393,7 @@ function operand(self, where, delta, data, val, op) {
// only increment the version if an array position changes.
// modifying elements of an array is ok if position does not change.

if (op === '$push' || op === '$pushAll' || op === '$addToSet') {
if (op === '$push' || op === '$addToSet') {
self.$__.version = VERSION_INC;
} else if (/^\$p/.test(op)) {
// potentially changing array positions
Expand Down Expand Up @@ -427,12 +426,8 @@ function handleAtomics(self, where, delta, data, value) {

if (typeof value.$__getAtomics === 'function') {
value.$__getAtomics().forEach(function(atomic) {
var op = atomic[0];
var val = atomic[1];
if (self.schema.options.usePushEach && op === '$pushAll') {
op = '$push';
val = { $each: val };
}
const op = atomic[0];
const val = atomic[1];
operand(self, where, delta, data, val, op);
});
return;
Expand Down
1 change: 0 additions & 1 deletion lib/services/query/castUpdate.js
Expand Up @@ -271,7 +271,6 @@ var numberOps = {

var castOps = {
$push: 1,
$pushAll: 1,
$addToSet: 1,
$set: 1,
$setOnInsert: 1
Expand Down
9 changes: 5 additions & 4 deletions lib/types/array.js
Expand Up @@ -202,7 +202,7 @@ MongooseArray.mixin = {

var selector;

if (op === '$pullAll' || op === '$pushAll' || op === '$addToSet') {
if (op === '$pullAll' || op === '$addToSet') {
atomics[op] || (atomics[op] = []);
atomics[op] = atomics[op].concat(val);
} else if (op === '$pullDocs') {
Expand All @@ -216,6 +216,9 @@ MongooseArray.mixin = {
selector = pullOp['_id'] || (pullOp['_id'] = {$in: []});
selector['$in'] = selector['$in'].concat(val);
}
} else if (op === '$push') {
atomics.$push = atomics.$push || { $each: [] };
atomics.$push.$each = atomics.$push.$each.concat(val);
} else {
atomics[op] = val;
}
Expand Down Expand Up @@ -314,9 +317,7 @@ MongooseArray.mixin = {
undefined, { skipDocumentArrayCast: true });
var ret = [].push.apply(this, values);

// $pushAll might be fibbed (could be $push). But it makes it easier to
// handle what could have been $push, $pushAll combos
this._registerAtomic('$pushAll', values);
this._registerAtomic('$push', values);
this._markModified();
return ret;
},
Expand Down
2 changes: 2 additions & 0 deletions migrating_to_5.md
Expand Up @@ -53,3 +53,5 @@ The above code does **not** work in 5.x, you **must** wrap the `$match` and `$sk
```javascript
MyModel.aggregate([{ $match: { isDeleted: false } }, { $skip: 10 }]).exec(cb);
```

* `$pushAll` is no longer supported and no longer used internally for `save()`, since it has been [deprecated since MongoDB 2.4](https://docs.mongodb.com/manual/reference/operator/update/pushAll/). Use `$push` with `$each` instead.
4 changes: 2 additions & 2 deletions test/model.test.js
Expand Up @@ -2553,7 +2553,7 @@ describe('Model', function() {
});
});

it('updating multiple Number $pushes as a single $pushAll', function(done) {
it('multiple number push() calls', function(done) {
var db = start(),
schema = new Schema({
nested: {
Expand Down Expand Up @@ -2584,7 +2584,7 @@ describe('Model', function() {
});
});

it('updating at least a single $push and $pushAll as a single $pushAll', function(done) {
it('multiple push() calls', function(done) {
var db = start(),
schema = new Schema({
nested: {
Expand Down

0 comments on commit 82aeba5

Please sign in to comment.