Hello everyone,
I’m trying to edit subdocuments with mongoose, but I’m having problems.
I have a “Producer” model that references the “User” model like this:
user: {
type: mongoose.Schema.Types.String,
ref: 'User',
required: true
},
This is the method I use to try to edit.
Producer.findByIdAndUpdate({ _id: req.body.id },
{"$set": {"user.$.name" : "John" }}, {"new": true}, function (err, doc) {
(err ? res.status(400).send(err) : res.status(200).json(doc));
}).populate('user');
I’m doing the test using the postman, using the PUT method.
This is the return I get:
I appreciate if anyone can help me analyze this!
In the “Producer” document, the reference to “User” looks like this:
user: ObjectId(“6238d433980fba1bc09ae359”)