I am trying to insert a new field 'description' with a findOneAndUpdate statement using Mongoose js. It is not working. The code works fine when the field already exists and updates it with the given value. According to the Mongodb documentation $set should create this field automatically if it does not exist.
Any ideas appreciated.
Course.findOneAndUpdate({'_id': CourseId},{$set:{'description':courseDescription}},function(err, course) {
if (err)
res.jsonp({response:'err'});
res.jsonp(course);
});
With mongoose you cannot set arbitrary fields, at least not in the way your trying to in your example.
You need to add the description to your Course
Schema and then your code example will work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With