Subdocument does not update - mongoose

I’m trying to update a subdocument of the parent document.

I have a document called “Post” and I reference the “User” document like this:

const PostSchema = new mongoose.Schema({
  title: String,
  user: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'User',
    required: true

  { collection: 'posts' })

const Post = mongoose.model('Post', PostSchema);
module.exports = Post;

I’m trying to change the name, for example of whoever posted it. The name field is in “User”.

I’m trying to change it this way:

exports.update = async (req, res) => {
//find user by its id, update its post with what's in req.body
Post.findById(, function(err, result) {
    if (!err) {
      if (!result){
        res.status(404).send('User was not found');
        result.user.nome = "User Name";
        result.markModified("user");, saveresult) {
          if (!saveerr) {
          } else {
    } else {

This is my route.js


When I run it on the postman, I get status 200 and the name appears modified in the return, but it is not saved in the bank.

I would appreciate it if someone could help me analyze it!

1 Like