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

114
Views
unable to updateMany inside forEach loop

I have an array that I would like to save into a Group collections contact array. My theory is to run a forEach loop over the array and " for each " element in the array run a command to push that element into the documents contact array.

When I run this nothing is happening. Im not seeing any errors, but it does NOT save in the database

Here is my function

// example of finished Matches array that I want to loop over and push each row into contacts array

const Matches = [
    "61380eeda0817b22c",
    "61380f14a0817b22c",
    "614c38608b931604b",
    "614c39598b931604b"
]
 const addContactsToGroup = async (req, res) => {
            const { groupId, selectedRows } = req.body

            const group = await Group.findById(groupId) //successfully got the groupId
               
            const Matches = selectedRows.filter(row => {
                    const isMatched = group.contacts.some(groupContacts => groupContacts === row)
                    return !isMatched
                })
                Matches.forEach(match => { // this is not working for some reason?
                    Group.updateMany({ '_id': groupId}, {$push: {contacts : match}})
                })
                res.status(201).json({message: "Successfully updated"})
        
            }```
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!