I need to add or remove object from array depends on condition based on assignUser
await healthPlanCardsCollection.findOneAndUpdate({ _id: cardId }, {
$cond: {
if: assignUser,
then: { $push: { assignedUsers: { userId: new ObjectId(user._id), ...user } } },
else: { $pull: { assignedUsers: { userId: new ObjectId(user._id) } } },
}
})
But in such case I've error: "MongoServerError: Unknown modifier: $cond. Expected a valid update modifier or pipeline-style update specified as an array"
"mongodb": "^4.2.0"
How could I rewrite it in more elegant way and do not have an error?