Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongodb mongoose js update not inserting new field

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);
    });
like image 542
noobie Avatar asked Jul 13 '15 07:07

noobie


1 Answers

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.

like image 78
real_ate Avatar answered Nov 13 '22 09:11

real_ate