Mongoose save() method not writing to database Mongoose save() method not writing to database mongoose mongoose

Mongoose save() method not writing to database


Assuming you are changing a mixed-type subdocument, you will need to call user.markModified('accounts') to let Mongoose know what's changed before you save.

However, if two of these operations happen concurrently, you could be losing data. I would recommend you use the findAndModify command, in conjunction with $elemMatch and $set operators.


In the line you're putting

for account in user['accounts']

you're creating a new variable, independent of the User model instance. You could use that variable to show the data (for example through a REST API) but not for modify the User model instance. aka. You are modifying another variable.

Instead, try to modify the user you're getting from the findOne() query and save it.