MongoError: Unknown modifier: $pushAll in node js

I am using "mongoose": "^4.12.1" and usage of .push was throwing the same error. Usage of usePushEach works for me. We can turn it on for all the schemaslike this :

const mongoose = require('mongoose');
mongoose.plugin(schema => { schema.options.usePushEach = true });
const Schema = mongoose.Schema; 
const SampleSchema = new Schema({
    attr1: type,
    attr2: type,
    attr2: type,
    attr4: [{
        type: Schema.Types.ObjectId,
        ref: 'ref_table_name'
    }] });

The $pushAll operator is no longer supported in Mongo 3.6.2 (or any newer versions from 3.6.x+).

You can do the following:

  • add the usePushEach: true option the Schema definition as in:

    new mongoose.Schema({
      username: String
    }, {
      usePushEach: true
    });
    
  • downgrade to Mongo 3.4.11(or any 3.4.x version)


Adding to the above answers with the following code to get you a clarity in implementing concat method. You don't need to downgrade MongoDB particularly.

Before resolving this issue, you need to keep in mind that $pushAll method has been deprecated for a long time so perhaps they get rid of it in version 3.5 and above.

But you can resolve this issue with the other alternative  — concat method.

My old code,

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

has been replaced with,

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

Even Azure’s own MongoDB implementation doesn’t support $pushAll.

Thank you.


In case anyone new stumbles on this, from mongoose 5xx, support for usePushEach: true option in schema definition was removed

You should instead use concat() where you'd normally use push when appending objects. In cases where concat() does not work (example where $inc is called either explicitly or implicitly) use unshift().