Home > OS >  Mongoose Array Field Can't Updating
Mongoose Array Field Can't Updating

Time:09-23

Here is my schema;

...
  group: [
    {
      type: mongoose.Types.ObjectId,
      ref: 'groups'
    }
  ],
...

I'am trying update data. I can update other fields but I can not update group field. My given data;

{
  name: 'John Wick',
  role: '631c8f39fb0d6300ebf2d0c1',
  group: [ '632c770cdabb9fbf70a84efa', '632c76e26fec98665363cc43' ]
}

Name and Role can update but group is not. What should I do?

Edit: Here is my update code;

User.findOneAndUpdate({_id},{{
  name: 'John Wick',
  role: '631c8f39fb0d6300ebf2d0c1',
  group: [ '632c770cdabb9fbf70a84efa', '632c76e26fec98665363cc43' ]
}},{}, (err,doc) => err ? console.log(err) : null)

CodePudding user response:

You have double {{ in the second argument of the query. That should be changed with single {:

User.findOneAndUpdate({ _id }, {
  // Data
})
``
  • Related