Xen_mar
Xen_mar

Reputation: 9702

Mongoose Model.update() no effect

I'm new to mongoose but as I'm following a tutorial I realized that Model.update() is not working as it should and I can't seem to figure out why. My set up is quite simple:

main.js

const UserSchema = new Schema({
    name: String,
})

const User = mongoose.model('user', UserSchema);

User.update({ name: 'Joe' }, { name: 'Alex' })
            .then(() => User.find({}))
            .then(users => {
                console.log(users);
            })

The console.log returns:

[ { _id: 5ec0xxxxxxxxx, name: 'Joe ', __v: 0 } ]

I can't really seem to wrap my head around why the record is not updating in the Db. Can someone help?

Upvotes: 0

Views: 54

Answers (1)

user13278024
user13278024

Reputation:

Watch out for typos, in your console.log statement it's 'Joe ' (with a trailing space) but in the query it's 'Joe', without spaces. The values are different, so there's no match and no record is updated.

Upvotes: 1

Related Questions