mongodb Mongo DB 中的保存和插入有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16209681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 13:10:18  来源:igfitidea点击:

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

mongodb

提问by user2093576

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

Mongo DB 中的保存和插入有什么区别?两者看起来一样

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

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

采纳答案by Rahul

Save Vs Insert :

保存与插入:

In your given examples, the behavior is essentially the same.

在您给出的示例中,行为本质上是相同的。

savebehaves differently if it is passed with an "_id" parameter.

save如果使用“_id”参数传递,则行为会有所不同。

For save, If the document contains _id, it will upsert querying the collection on the _idfield, If not, it will insert.

对于保存,如果文档包含_id,它将在_id字段上查询集合,否则将插入。

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.

如果具有指定 _id 值的文档不存在,则 save() 方法使用文档中的指定字段执行插入。

如果存在具有指定 _id 值的文档,则 save() 方法执行更新,用文档中的字段替换现有记录中的所有字段。



Save vs Update:

保存与更新

updatemodifies an existing document matched with your query params. If there is no such matching document, that's when upsertcomes in picture.

update修改与您的查询参数匹配的现有文档。如果没有这样的匹配文件,那就是upsert图片。

  • upsert : false: Nothing happens when no such document exist
  • upsert : true: New doc gets created with contents equal to query params and update params
  • upsert : false: 当不存在这样的文件时什么也不会发生
  • upsert : true: 新文档被创建,内容等于查询参数和更新参数

save: Doesn't allow any query-params. if _idexists 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.

save:不允许任何查询参数。如果_id存在并且有相同的匹配文档_id,它会替换它。当没有指定_id/没有匹配的文档时,它会将文档作为新文档插入。

回答by squiroid

Let us consider the two cases here for save :-

让我们考虑这里的两种情况以进行保存:-

1) Having _id in doc.

1)在文档中有_id。

2) Not having _id in doc.

2) 文档中没有 _id。

                        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.

1) 在集合中有 _id 的文档。

2) Not having _id of doc in collection.

2) 集合中没有 _id 文档。

                        Insert()
                       /        \
                      /          \

   Doc Having _id in collection    Doc Not Having _id 
  ->  E11000 duplicate key     ->Insert a new doc inside the collection.
      error index:       

回答by AlphaB

saveinsert or update a document.

save插入或更新文档。

insertdoes only an insertion.

insert只做一个插入。

But in your case, it will do the same, as the document provided in save has no _idfield.

但在您的情况下,它会做同样的事情,因为 save 中提供的文档没有_id字段。

回答by Abhi

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

用之前保存的苹果的 _id 保存一个苹果

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

用_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

Apple 被插入,因为没有具有相同 Object Id 的 Apple 进行更新

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.

因此,如果提供了对象 id,则 save 将充当更新,前提是对象 id 已经存在,否则它会进行插入。

回答by 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.

如果您尝试将“插入”与以前在同一集合中使用过的 ID 一起使用,您将收到重复键错误。如果您对已在同一集合中的 ID 使用“保存”,它将被更新/覆盖。

If 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.

如果您希望进行真正的更新,我建议您使用“更新”。如果您使用集合中已有的相同 ID 进行保存,则更新不会以保存的方式覆盖。

For 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.

例如,您有两个字段“x”和“y”,您想保留这两个字段但更改“x”的值。如果您选择了“保存”命令并且没有将 y 包含在先前的值中或在您的保存中根本没有 y,那么 y 将不再具有相同的值或存在于那里。但是,如果您选择使用 $set 进行更新并且仅在更新语句中包含 x,则不会影响 y。

回答by Adam Comerford

As you can see here, the save method will essentially do an upsert (update if it finds the doc, insert otherwise):

正如你在这里看到的,save 方法本质上会执行一个 upsert(如果找到文档则更新,否则插入):

http://docs.mongodb.org/manual/reference/method/db.collection.save/#db.collection.save

http://docs.mongodb.org/manual/reference/method/db.collection.save/#db.collection.save

Insert is just that, a straight insert.

插入就是这样,一个直插入。

回答by Bravo

Consider the below document

考虑以下文件

{ "_id" : 1, "domainName" : "test1.com", "hosting" : "hostgator.com" }

if db already contains the document with _id:1, then

如果 db 已经包含 _id:1 的文档,则

save operation will throw the exception like below

保存操作将抛出如下异常

E11000 duplicate key error index ...........

and where as insert operation , will just override the document.

而作为插入操作,只会覆盖文档。

回答by Vijet Badigannavar

db.<collection_name>.save(<Document>)is equivalent to InsertOrUpdate Query.

db.<collection_name>.save(<Document>)相当于 InsertOrUpdate 查询。

While, db.<collection_name>.insert(<Document>)is equivalent to just Insert Query.

而,db.<collection_name>.insert(<Document>)相当于只是插入查询。

回答by Jagan

In terms of ORACLE: mongo insert => Oracle insert mongo save => Oracle merge

在ORACLE方面:mongo insert => Oracle insert mongo save => Oracle merge