'Mongoose won't update new field after updating schema declaration

I just noticed that when i update my schema definition and add a field, for instance "name: String" and then try to use

People.update( { _id: user_id }, { $set: { name: 'something' } } ) 

mongoose won't update my property.

I keep getting nModified: 0 on the response.

The only way i found to fix it, is to Drop the collection and then the new documents will work perfectly.

Am i missing something? Does mongoose somehow "caches" the schema of a collection on mongodb itself and then needs a "drop" in order to "reload" the properties?



Solution 1:[1]

I think findbyidandupdate will do the task for you. Try with this link

Mongoose - findByIdAndUpdate - doesn't work with req.body

Solution 2:[2]

Can you please share your People model and also please use callback with update.

see below and works fine..

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');

var User = mongoose.model('User', { email: String, name: String });
//First added only email and used save to save the document.
//var user = new User({ email: '[email protected]' });
User.update({ _id: '55fbbb268e7307dc0bf9ae92' }, { $set: { name: 'John Due' }}, function(err, result) {
    if(err) throw err;
    console.log(result)
 });

Solution 3:[3]

Stop your node app and restart the app. It should work.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Community
Solution 2 kj007
Solution 3 adi