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

427
Views
Mongodb 4.4.x - findOneAndUpdate() with $set and $push

I am trying to do a findOneAndUpdate() to Find a document and update it in one atomic operation

db.characters.insertOne({ movies: ['Star Trek'], name: 'Jean-Luc Picard' })

db.characters.findOneAndUpdate({ name: 'Jean-Luc Picard' }, [
    { $set: { age: 59 } },
    { $push: { movies: 'Next Gen' } }
])

gives error

uncaught exception: Error: findAndModifyFailed failed: {
    "ok" : 0,
    "errmsg" : "Unrecognized pipeline stage name: '$push'",
    "code" : 40324,
    "codeName" : "Location40324"
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DBCollection.prototype.findAndModify@src/mongo/shell/collection.js:742:15
DBCollection.prototype.findOneAndUpdate@src/mongo/shell/crud_api.js:910:12
@(shell):1:1

Is there a different syntax to push element to an array? I also tried $addToSet which did not work either.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Try this, probably will work:

db.characters.findOneAndUpdate({ name: 'Jean-Luc Picard' }, 
{ $set: { age: 59 }, $push: { movies: 'Next Gen' } })
over 4 years ago · Santiago Trujillo Report
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!