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.

Mongoose not saving nested documents

See original GitHub issue

Prerequisites

  • 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

image 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:closed
  • Created a year ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
vkarpov15commented, Sep 30, 2022

@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.

1reaction
IslandRhythmscommented, Sep 14, 2022

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.

Read more comments on GitHub >

github_iconTop 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 >

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