MongoError: Unknown modifier: $pushAll in node js

I am having an issue when saving a model fails with mongo error: MongoError: Unknown modifier: $pushAll.

I have one array field subDomains in my schema and which will get saved as default with subdomain as like follows.

 // already Domain instance get availble 
 Domain.subDomains.push({'name': 'default' , 'role': 'xyz', ...});

 // save domain with default fileds
 Domain.save() 

System Info as like as follows:

         ?  ~ node --version
          v9.4.0
          ?  ~ npm --version
          5.6.0
          ?  ~ 
          ?  ~ mongo --version
            MongoDB shell version v3.6.2
            git version: ......
            OpenSSL version: OpenSSL 1.0.2n  7 Dec 2017
            allocator: system
            modules: none
            build environment:
                distarch: x86_64
                target_arch: x86_64
          ?  ~ 

Please help me to sort out this one.

Answers:

Answer

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:

Answer

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.

Answer

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().

Answer

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'
    }] });
Answer

Replacing .push with .unshift is the best way to deal with this error. I hope this might save your day.

Answer

If you are using > 3.6 It's not working. But if you would make it work, you can use $push along with $each it'll work.

Answer

use concat solve the problem

 var newArray=  Domain.subDomains.concat([{'name': 'default' , 'role': 'xyz'}]) 
      Domain.subDomains=newArray
      Domain.save()
Answer

try

Domain._doc.subDomains.push({'name': 'default' , 'role': 'xyz', ...});

this works for me

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.