Mongoose: Replace deprecated mongodb $pushAll operator with $push + $each

Created on 24 Aug 2016  ·  13Comments  ·  Source: Automattic/mongoose

According to MongoDB documentation $pushAll has been deprecated since version 2.4. However, it remains the operator Array#push translates into.

On the assumption that is not a breaking change, I think it would be a convenient fix, since some implementation may not rely on $pushAll. In fact, for what it is worth, Azure's own MongoDB implementation does not support $pushAll operator AFAIK .

Most helpful comment

This may mitigate the impact of Array.push relying on $pushAll. I tried it myself and seems to work.

myArray.push(myObject); //breaks on DocumentDB with Mongo API because of $pushAll

instead use

myArray = myArray.concat([myObject]); //this uses $set so no problems

@vkarpov15 Supporting DocumentDB with Mongo Protocol (so not DocumentDB as it is) would be fantastic, even though I can understand that project priorities are on Mongo. Sadly Microsoft seem to have implemented just a fair subset of the latest Mongo API (3.x), so deprecated stuff that comes from 2.x may not work.

All 13 comments

Getting off of pushAll is definitely a priority, but fair warning, mongoose does _not_ support documentdb at the moment, so use it at your own risk

This may mitigate the impact of Array.push relying on $pushAll. I tried it myself and seems to work.

myArray.push(myObject); //breaks on DocumentDB with Mongo API because of $pushAll

instead use

myArray = myArray.concat([myObject]); //this uses $set so no problems

@vkarpov15 Supporting DocumentDB with Mongo Protocol (so not DocumentDB as it is) would be fantastic, even though I can understand that project priorities are on Mongo. Sadly Microsoft seem to have implemented just a fair subset of the latest Mongo API (3.x), so deprecated stuff that comes from 2.x may not work.

In 4.6.4 you'll be able to use the usePushEach option in schemas: new Schema({ arr: [String] }, { usePushEach: true });

Started running into this problem on Mongo 3.6 and Mongoose 4.13.6, message: 'Unknown modifier: $pushAll',

Suggested fix does not work:

var layerSchema = mongoose.Schema({
   (...) 
}, 
{ 
    usePushEach: true 
});

It's weird that this is still an issue with the updated libs?

Same as Knutole here, I'm creating models with mongoose and I don't know if / where I can plugin that fix.

This suggestion solve the problem in mongodb 3.6 for me.

Edit 1:
Forget, it didn't work here too.

Edit 2:
The solution was keep running on mongo 3.4

Same issue here using Mongo 3.6
Any push and save on array field generate the error: Unknown modifier: $pushAll
Using the latest Mongoose too...

Workaround usePushEach: true seems to work for us. MongoDB 3.6 + db.adminCommand({ setFeatureCompatibilityVersion: "3.6" })

@knutole what issue are you running into? This solution has worked for me. @frguthmann safest bet would be to add that line to all models if you're using MongoDB 3.6+.

Mongo 3.6 was recently released and removed support for $pushAll, so everyone downloading the latest and greatest mongo will run into this.

The solution here is to either

1) Use MongoDB V3.4.X (or 3.5 for that matter, but it's an unstable dev release)

OR

2) add usePushEach: true option to your models that will require this functionality.

I recommend #1 for production apps.

@knutole That solution worked fine for me. Using v3.6

var aSchema = new mongoose.Schema({
    ...
}, {
        usePushEach: true 
    }
);

Gives no errors.

There's a fix for this in the upcoming 5.0.0-rc0 release, for now just use the usePushEach option.

Fix works in the 5.0.0-rc2 release.

Was this page helpful?
0 / 5 - 0 ratings