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

55
Views
Update with findOneAndUpdate does not run

I'm trying to update using the ID of the object inside the array but I can't do it passing the objects in $set through a variable. This is my code that I can't get to work:

        const itemToUpdate = {
            concept: req.body.concept,
            incomeAmount: req.body.incomeAmount,
            description: req.body.description,
            expenseAmount: req.body.expenseAmount,
            'createdBy':{
                uid: req.uid,
                username: 'poshed',
            }
        };
    
    let pettyCashItem = await PettyCashItems.findOneAndUpdate({
        _id: id, 
        "items._id": idItem },
    {
        $set: { itemToUpdate }
    }, { new: 'true'});

But it happens that if I do it in the following way indicating object pot object it works but you have to write one by one:

    let pettyCashItem = await PettyCashItems.findOneAndUpdate({
        _id: id, 
        "items._id": idItem },
    {
        $set: { "items.$.concept": 'Test' }
    }, { new: 'true'});

Thanks!

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!