Home:ALL Converter>What is the difference between save and insert in Mongo DB?

What is the difference between save and insert in Mongo DB?

Ask Time:2013-04-25T16:17:10         Author:user2093576

Json Formatter

What is the difference between save and insert in Mongo DB? both looks the same

db.users.save({username:"google",password:"google123"})

db.users.insert({username:"google",password:"google123"})

Author:user2093576,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/16209681/what-is-the-difference-between-save-and-insert-in-mongo-db
Aurélien B :

save insert or update a document.\n\ninsert does only an insertion.\n\nBut in your case, it will do the same, as the document provided in save has no _id field.",
2013-04-25T08:21:09
Abhi :

By giving an example \n\nSave an Apple\n\ndb.fruit.save({\"name\":\"apple\", \"color\":\"red\",\"shape\":\"round\"})\nWriteResult({ \"nInserted\" : 1 })\n\ndb.fruit.find();\n\n{\n \"_id\" : ObjectId(\"53fa1809132c1f084b005cd0\"),\n \"color\" : \"red\",\n \"shape\" : \"round\",\n \"name\" : \"apple\"\n}\n\n\nSave an apple with _id of previously saved apple\n\ndb.fruit.save(\n{\"_id\" : ObjectId(\"53fa1809132c1f084b005cd0\"),\"name\":\"apple\", \n\"color\":\"real red\",\"shape\":\"round\"})\n\nWriteResult({ \"nMatched\" : 1, \"nUpserted\" : 0, \"nModified\" : 1 })\n\n\nNow the apple we saved has, color updated from red to real red \n\ndb.fruit.find();\n{\n \"_id\" : ObjectId(\"53fa1809132c1f084b005cd0\"),\n \"color\" : \"real red\",\n \"shape\" : \"round\",\n \"name\" : \"apple\"\n}\n\n\nSave an apple with _id\n\ndb.fruit.save({\"_id\" : ObjectId(\"55551809132c1f084b005cd0\"),\n\"name\":\"apple\", \"color\":\"real red\",\"shape\":\"round\"})\n\n WriteResult({ \"nMatched\" : 0, \"nUpserted\" : 1, \n\"nModified\" : 0, \"_id\": 55551809132c1f084b005cd0 })\n\n\nApple got inserted as there is no apple with the same Object Id to do an update\n\nInsert an Orange\n\ndb.fruit.insert({\"name\":\"orange\", \"color\":\"orange\",\"shape\":\"round\"})\nWriteResult({ \"nInserted\" : 1 })\n\n\nOrange is inserted \n\ndb.fruit.find();\n{\n \"_id\" : ObjectId(\"53fa1809132c1f084b005cd0\"),\n \"color\" : \"real red\",\n \"shape\" : \"round\",\n \"name\" : \"apple\"\n}\n{\n \"_id\" : ObjectId(\"53fa196d132c1f084b005cd7\"),\n \"color\" : \"orange\",\n \"shape\" : \"round\",\n \"name\" : \"orange\"\n}\n{\n \"_id\" : ObjectId(\"55551809132c1f084b005cd0\"),\n \"color\" : \"real red\",\n \"shape\" : \"round\",\n \"name\" : \"apple\"\n}\n\n\nSo save will act as an update if supplied with an object id, provided the object id already exists other wise it does an insert. ",
2014-08-24T17:12:05
RoganRicheart :

If you attempt to use \"insert\" with an ID that was previously used in the same collection you will get a duplicate key error. If you use \"save\" with an ID that is already in the same collection, it will get updated/overwritten.\n\nIf you are looking to do a true update I would suggest using \"update\". Update will not overwrite in the way Save would if you are Saving using the same ID that is already in the collection.\n\nFor example you have two fields \"x\" and \"y\" and you want to keep both but change the value of \"x\". If you chose the \"save\" command and did not include y with the previous value or not have y at all in your save, then y would no longer have the same value or be there. However if you chose to update using $set and only had x included in your update statement, you would not affect y. ",
2014-05-26T02:49:08
Adam Comerford :

As you can see here, the save method will essentially do an upsert (update if it finds the doc, insert otherwise):\n\nhttp://docs.mongodb.org/manual/reference/method/db.collection.save/#db.collection.save\n\nInsert is just that, a straight insert.",
2013-04-25T08:21:33
Bravo :

Consider the below document \n\n{ \"_id\" : 1, \"domainName\" : \"test1.com\", \"hosting\" : \"hostgator.com\" }\n\n\nif db already contains the document with _id:1, then\n\nsave operation will throw the exception like below \n\nE11000 duplicate key error index ...........\n\n\nand where as insert operation , will just override the document.",
2014-06-03T09:01:18
Jagan :

In terms of ORACLE:\nmongo insert => Oracle insert\nmongo save => Oracle merge",
2014-02-17T12:05:04
Rahul :

Save Vs Insert :\nIn your given examples, the behavior is essentially the same.\nsave behaves differently if it is passed with an "_id" parameter.\nFor save, If the document contains _id, it will upsert querying the collection on the _id field, If not, it will insert.\n\nIf a document does not exist with the specified _id value, the save() method performs an insert with the specified fields in the document.\nIf 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.\n\n\nSave vs Update :\nupdate modifies an existing document matched with your query params. If there is no such matching document, that's when upsert comes in picture.\n\nupsert : false : Nothing happens when no such document exist\nupsert : true : New doc gets created with contents equal to query params and update params\n\nsave : 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.",
2013-04-25T08:26:10
squiroid :

Let us consider the two cases here for save :-\n\n1) Having _id in doc.\n\n2) Not having _id in doc.\n\n Save ()\n / \\\n / \\\n\n Having _id Not Having _id \n\n ->In this case save will do -> It will do normal insertion \n upsert to insert.Now in this case as insert() do.\n what that means, it means \n take the document and replace \n the complete document having same\n _id.\n\n\nLet us consider the two cases here for insert:-\n\n1) Having _id of doc in collection.\n\n2) Not having _id of doc in collection.\n\n Insert()\n / \\\n / \\\n\n Doc Having _id in collection Doc Not Having _id \n -> E11000 duplicate key ->Insert a new doc inside the collection.\n error index: \n",
2015-03-31T05:59:49
yy