question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Array SubDocs don't save on `document.save()`

See original GitHub issue

It seems that array subdocs don’t save on top-level document.save(). They only persist their changes on findOneAndUpdate/findByIdAndUpdate/ any update operation that bypasses document validation, and only when using $set.

Here’s a repo to reproduce:

https://github.com/varunjayaraman/mongoose-save-array-bug/tree/master

Edit: Sorry, forgot to mention This error occurred on v4.6.0 and v4.5.2

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
VictorGerritsenQLVRcommented, Sep 20, 2016

I’ve had a similar problem in the past and managed to solve it by marking that property as modified using:

parent.markModified('children')
parent.save();
1reaction
sobafuchscommented, Feb 15, 2017

@numbergames basically change detection on modifying array indexes using something like parentDoc.subDoc[index] = value won’t work. You have to use document.set() instead

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mongoose doesn't save nested sub documents - Stack Overflow
I need to save some sub documents in a schema which is sub document of a schema. The save function is this: exports.add...
Read more >
Mongoose v6.8.1: SubDocuments
Subdocuments have save and validate middleware just like top-level documents. Calling save() on the parent document triggers the save() middleware for all ...
Read more >
Mongoose 101: Working with subdocuments - Zell Liew
The easiest way to update subdocuments is: Use findOne to find the document; Get the array; Change the array; Run save. For example,...
Read more >
Accidentally Clicked "Don't Save" in Microsoft WordIntegris
Accidentally clicked "don't save" in Microsoft Word: Four Options to Help Find Unsaved Documents By Integris.
Read more >
Mongoose 101: An Introduction to the Basics, Subdocuments ...
Let's say we want to save two things into the database: ... findOne : Gets one document. find : Gets an array of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found