Mongoose not saving nested documents
See original GitHub issuePrerequisites
- I have written a descriptive issue title
- I have searched existing issues to ensure the bug has not already been reported
Mongoose version
5.12.14
Node.js version
v16.13.1
MongoDB server version
5.0.11
Description
When modifying a field on a populated document, changes should be saved, when saving the parent.
This is not occ, instead I need to save the document myself (see Steps to Reproduce)
Steps to Reproduce
Schemas
const boardSchema = new Schema({
name: {
type: String,
required: true
},
tasks: [
{
type: Schema.Types.ObjectId,
ref: 'Task'
}
]
})
const taskSchema = new mongoose.Schema({
name: {
type: String
},
description: String
})
route to change name
router.patch('/:boardId/:taskId', async (req, res) => {
const { boardId, taskId } = req.params
const { value } = req.body
const board = await Board.findById(boardId).populate('tasks')
board.tasks.find(x => x._id.toString() === taskId).name = value
//await board.tasks[0].save() // <--------- Saving changes to task[0] // THIS SHOULD NOT BE REQUIRED
await board.save() // <-------------------- Not saving changes to nested tasks (e.g. the name)
res.json(board) // <----------------------- Showing correct change
res.send('OK')
})
Expected Behavior
https://mongoosejs.com/docs/subdocs.html#subdocuments
Saving the tasks individually should not be required. They should be saved when the parent is being saved
Issue Analytics
- State:
- Created a year ago
- Comments:5
Top Results From Across the Web
Mongoose not saving nested object - Stack Overflow
To "tell" Mongoose that the value of a Mixed type has changed, call the .markModified(path) method of the document passing the path to...
Read more >[Solved]-Mongoose doesn't save nested Object in another ...
Coding example for the question Mongoose doesn't save nested Object in another nested Object-mongodb.
Read more >Updating a nested object in a document using mongoose
i am suspecting that MongoDB is applying query conditions on collection and returns the result with the matching documents so its probably ...
Read more >Mongoose v6.8.1: SubDocuments
Subdocuments are similar to normal documents. Nested schemas can have middleware, custom validation logic, virtuals, and any other feature top-level schemas can ...
Read more >MongoDB in NodeJS - Nested Documents (2020) [Episode #14]
In this video we go over how you can insert and read nested objects inside of a MongoDB document using Node JS.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@jamesbruice when you populate a
ref
, you get a populated document, which is a distinct top-level document, rather than a subdocument embedded in your document. The difference is that an embedded subdocument is stored in MongoDB as a subdocument, rather than as an ObjectId that refers to another document.A ref indicates what Model populate should look at when querying the indicated path. A subdocument is described in the image you attached to this issue.