0 votes
1 view
in Web Technology by (47.7k points)

To modify a field in an existing entry in mongoose, what is the difference between using

model = new Model([...]) 

model.field = 'new value'; 

model.save();

and this

Model.update({[...]}, {$set: {field: 'new value'});

The reason I'm asking this question is because of someone's suggestion to an issue I posted yesterday: NodeJS and Mongo - Unexpected behaviours when multiple users send requests simultaneously. The person suggested to use update instead of saving, and I'm not yet completely sure why it would make a difference.

Thanks!

1 Answer

0 votes
by (107k points)

You can understand the difference between these two concepts by reading below answer:-

The .save() you already have an object in your client-side code or had to retrieve the data from the server before you are writing it back, and you are writing back the whole thing.

On the other hand .update() does not require the data to be loaded to the client from the server. All of the interaction happens server-side without retrieving to the client. So .update() can be very efficient in this way when you are adding content to existing documents.

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...