In case of save method, checks if there is a document with the same _id as the one you save exists. When it exists, it replaces it. When no such document exists, it inserts the document as a new one.
while update,changes an existing document found by find-parameters and does nothing when no such document exist
The update() method updates values in the existing document.
syntax for update command:
>db.Collection_name.update(Selection_Criteria,Update_data);
Consider the mycol collectioin has following data.
>db.Collection_name.find();
{ "_id" : ObjectId(5983548781331adf45ec5), "title":"Queryhome"};
{ "_id" : ObjectId(5983548781331adf45ec6), "title":"Tech Home"};
{ "_id" : ObjectId(5983548781331adf45ec7), "title":"GK-Tech-Chill"};
then
db.Collection_name.update({"title":"Queryhome"},{$set:{"title":"Queryhome media solutions"}});
then execute this command
>db.Collection_name.find();
you will see-
{ "_id" : ObjectId(5983548781331adf45ec5), "title":"Queryhome media solutions"};
{ "_id" : ObjectId(5983548781331adf45ec6), "title":"Tech Home"};
{ "_id" : ObjectId(5983548781331adf45ec7), "title":"GK-Tech-Chill"};
But In case of save:
The save() method replaces the existing document with the new document passed in save() method.
>db.Collection_name.save({_id:ObjectId(),NEW_DATA});
Following example will replace the document with the _id '5983548781331adf45ec7'
>db.Collection_name.save(
{
"_id" : ObjectId(5983548546431adf45ec7), "title":"Tech Solutions",
"by":"Queryhome"
}
)
>db.Collection_name.find()
{ "_id" : ObjectId(5983548546431adf45ec7), "title":"Tech Solutions",
"by":"Queryhome"}
{ "_id" : ObjectId(5983548781331adf45ec6), "title":"GK home"}
{ "_id" : ObjectId(5983548781331adf45ec7), "title":"Tech-GK-Chill"}