denislexic
denislexic

Reputation: 11392

Mongoose/Mongodb using $set vs just setting the values in update queries

Is there a difference, when updating a value in mongodb/mongoose between using $set:

await Users.findOneAndUpdate({_id:"xxx"},{$set: {name:"Denis"} })

and just setting it like this:

await Users.findOneAndUpdate({_id:"xxx"},{name:"Denis"})

Thanks.

Upvotes: 5

Views: 1002

Answers (1)

Cuong Le Ngoc
Cuong Le Ngoc

Reputation: 11975

In the doc, it mentioned that:

By default, if you don't include any update operators in doc, Mongoose will wrap doc in $set for you. This prevents you from accidentally overwriting the document.

So there is no difference unless you specify overwrite: true in options.

Upvotes: 10

Related Questions