In MongoDB, the 'update ()' and 'save ()' methods are used to update the document into a collection. The 'update ()' method is used to update the values present in the existing document whereas 'save ()' method replaces the entire existing document with the new document which is passed in the 'save ()' method.
In MongoDB, the insert() method inserts a document or documents into the collection. It takes two parameters, the first parameter is the document or array of the document that we want to insert and the remaining are optional.
MongoDB's update() and save() methods are used to update document into a collection. The update() method updates the values in the existing document while the save() method replaces the existing document with the document passed in save() method.
Model. create does a . save for each document in the array, resulting in N database calls (where N is the number of documents in the array); Collection. insert performs one large database call.
Save Vs Insert :
In your given examples, the behavior is essentially the same.
save
behaves differently if it is passed with an "_id" parameter.
For save, If the document contains _id
, it will upsert querying the collection on the _id
field, If not, it will insert.
If a document does not exist with the specified _id value, the save() method performs an insert with the specified fields in the document.
If a document exists with the specified _id value, the save() method performs an update, replacing all field in the existing record with the fields from the document.
Save vs Update :
update
modifies an existing document matched with your query params. If there is no such matching document, that's when upsert
comes in picture.
upsert : false
: Nothing happens when no such document existupsert : true
: New doc gets created with contents equal to query params and update paramssave
: Doesn't allow any query-params. if _id
exists and there is a matching doc with the same _id
, it replaces it. When no _id specified/no matching document, it inserts the document as a new one.
Let us consider the two cases here for save :-
1) Having _id in doc.
2) Not having _id in doc.
Save ()
/ \
/ \
Having _id Not Having _id
->In this case save will do -> It will do normal insertion
upsert to insert.Now in this case as insert() do.
what that means, it means
take the document and replace
the complete document having same
_id.
Let us consider the two cases here for insert:-
1) Having _id of doc in collection.
2) Not having _id of doc in collection.
Insert()
/ \
/ \
Doc Having _id in collection Doc Not Having _id
-> E11000 duplicate key ->Insert a new doc inside the collection.
error index:
save
insert or update a document.
insert
does only an insertion.
But in your case, it will do the same, as the document provided in save has no _id
field.
By giving an example
Save an Apple
db.fruit.save({"name":"apple", "color":"red","shape":"round"})
WriteResult({ "nInserted" : 1 })
db.fruit.find();
{
"_id" : ObjectId("53fa1809132c1f084b005cd0"),
"color" : "red",
"shape" : "round",
"name" : "apple"
}
Save an apple with _id of previously saved apple
db.fruit.save(
{"_id" : ObjectId("53fa1809132c1f084b005cd0"),"name":"apple",
"color":"real red","shape":"round"})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Now the apple we saved has, color updated from red to real red
db.fruit.find();
{
"_id" : ObjectId("53fa1809132c1f084b005cd0"),
"color" : "real red",
"shape" : "round",
"name" : "apple"
}
Save an apple with _id
db.fruit.save({"_id" : ObjectId("55551809132c1f084b005cd0"),
"name":"apple", "color":"real red","shape":"round"})
WriteResult({ "nMatched" : 0, "nUpserted" : 1,
"nModified" : 0, "_id": 55551809132c1f084b005cd0 })
Apple got inserted as there is no apple with the same Object Id to do an update
Insert an Orange
db.fruit.insert({"name":"orange", "color":"orange","shape":"round"})
WriteResult({ "nInserted" : 1 })
Orange is inserted
db.fruit.find();
{
"_id" : ObjectId("53fa1809132c1f084b005cd0"),
"color" : "real red",
"shape" : "round",
"name" : "apple"
}
{
"_id" : ObjectId("53fa196d132c1f084b005cd7"),
"color" : "orange",
"shape" : "round",
"name" : "orange"
}
{
"_id" : ObjectId("55551809132c1f084b005cd0"),
"color" : "real red",
"shape" : "round",
"name" : "apple"
}
So save will act as an update if supplied with an object id, provided the object id already exists other wise it does an insert.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With