Skip to content
Advertisement

Looping Over And Array of Objects and Updating Each One

I’m trying to update objects inside an array based on specific conditions that I check using two functions. I couldn’t achieve this with the match feature available in mongoose.

As you can see below, I’m trying to loop over the array of posts, send each post and check if it fulfills a condition, then update it using findOneAndUpdate. The functions isLessThanOne and isAchieved return the expected value.

JavaScript

This is the schema

JavaScript

The problem is that I couldn’t update the posts and I’ve already tried using udpate, directly updating it with post.category then save. Any help would appreciated.

Advertisement

Answer

Try this:

JavaScript

If I recall, the filter parameter must be an object.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement