2

I am trying to use mongoose's findByIdAndUpdate method to pass in a list of object id's and updating them at once. However, I am getting a "Error: Can't use $set with ObjectId." error which I can't seem to associate with my code.

Here's my code.

return ComponentModel.findByIdAndUpdate({
    _id: {
        $in: input.subjectIds
    },
    $set: { location: input.newLocation }
}).then(res => res);
cyberbeast
  • 678
  • 1
  • 12
  • 30

1 Answers1

4

findByIdAndUpdate is for one document. For multiple documents you can use update with multi flag true.

return ComponentModel.update(
 {_id: {$in: input.subjectIds}},
 {$set: {location: input.newLocation}},
 {"multi": true}
).then(res => res);
Talha Awan
  • 4,573
  • 4
  • 25
  • 40