Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

213
Views
Mongodb findAndUpdate elements in array with condition

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?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!