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"})
}```