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

Viewed 194

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.

1 Answers

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.

Related