0

$[] (positional-all) update operator has been added in mongodb version 3.6. but it is not working in mongoose.

I want to update all the elements of allTasks array of a particular user, whose paid : 0 to paid :1

The query which i am using is :

User.update({
        _id: userId,
        "allTasks.paid":0
    },{
        $set : {
            "allTasks.$[].paid":1
        }
    })

UserSchema

var UserSchema = mongoose.Schema({
    allTasks: [{
        paid: Number // 0: unpaid, 1: paid
        //other fields
    }],
    //other fields
});

The above given query is not working, can anyone tell me, if i am missing something, or doing something wrong?

Ravi Shankar Bharti
  • 8,922
  • 5
  • 28
  • 52

1 Answers1

0

For anyone else who is facing the same issue:

The issue was Mongodb & mongoose version.

  • MongoDB version should be 3.6+
  • Mongoose version needs to be 5.x or more

Also you might need to use setFeatureCompatibilityVersion : 3.6.

From this answer:

When switching from lower version to higher version for mongodb you have to set setFeatureCompatibilityVersion for your mongodb which

Enables or disables the features that persist data incompatible with earlier versions of MongoDB. You can only issue the setFeatureCompatibilityVersion against the admin database.

You can simply set this by running this command in mongo shell

db.adminCommand( { setFeatureCompatibilityVersion: "3.6" } )
Ravi Shankar Bharti
  • 8,922
  • 5
  • 28
  • 52