2013-04-25 148 views
120

在Mongo DB中保存和插入有什麼區別? 兩個看起來相同在Mongo DB中保存和插入有什麼區別?

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

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

http://docs.mongodb.org/manual/reference/method/db.collection.save/#db.collection.save – 2014-06-26 11:23:31

+0

fyi save()現已在pymongo中棄用。 – 2018-03-08 00:46:18

回答

121

保存Vs的插入:

在你給出的例子,其行爲本質上是一樣的。

save如果與「_id」參數一起傳遞,其行爲將有所不同。

對於保存,如果文檔包含_id,它將插入查詢_id字段上的集合,否則將插入。

如果文檔不存在指定的_id值,那麼save()方法會執行一個帶有文檔中指定字段的插入。

如果文檔以指定的_id值存在,則save()方法執行更新,將現有記錄中的所有字段替換爲文檔中的字段。


保存VS更新

update修改與您的查詢參數匹配現有的文檔。如果沒有這樣的匹配文檔,那麼當upsert進入圖片。

  • upsert : false:什麼也沒有發生的時候沒有這樣的文件存在
  • upsert : true:新文檔中獲取與等於查詢參數和更新PARAMS

save內容創建:不允許任何查詢 - PARAMS。如果_id存在並且有一個匹配的文檔與_id相同,則替換它。當沒有_id指定/沒有匹配的文檔時,它將文檔作爲新文檔插入。

+14

保存vs更新upsert:true? – Jeff 2014-02-14 12:40:43

+8

都有不同的語法。更新需要多個參數({condition},{update to doc},upsert,multi),而save僅接受一個參數(_id是條件參數的參數).update可以接受任何條件,但保存僅限於條件_id字段。 – Rahul 2014-02-17 04:33:31

+1

從版本2.6開始,保存有第二個參數,它表示寫入關注的文檔。 http://docs.mongodb.org/manual/reference/method/db.collection.save/ – huggie 2014-07-26 09:00:47

30

save插入或更新文檔。

insert只做插入。

但在你的情況下,它會做同樣的事情,因爲保存中提供的文檔沒有_id字段。

1

在ORACLE方面: 蒙戈插入=>甲骨文插入 蒙戈保存=>甲骨文合併

10

如果您嘗試使用「插入」,與以前相同的集中使用,你會得到一個ID重複鍵錯誤。如果您將「保存」與已在同一個集合中的ID一起使用,則它將被更新/覆蓋。

如果你正在尋找做一個真正的更新,我會建議使用「更新」。如果您使用與集合中已存在的相同ID進行保存,則更新不會以Save方式覆蓋。

例如,您有兩個字段「x」和「y」,並且要保留兩個字段但更改「x」的值。如果你選擇了「保存」命令,並且沒有將y與前面的值一起包含,或者你的保存中沒有y,那麼y將不再具有相同的值或存在。但是,如果您選擇使用$ set進行更新,並且只有x包含在更新語句中,則不會影響y。

3

考慮以下文件

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

如果數據庫已經包含_id文件:1,然後

保存操作,將拋出異常像下面

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

,並在那裏爲插入操作,只會覆蓋文檔。

+0

如果在數據庫中已經存在具有相同_id的 文檔,則'db.collection.save()'方法更新文檔。當數據庫中已存在具有相同_id 的文檔時,保存方法完全將文檔替換爲 新文檔。 Pro-Book MongoDB Development – 2016-01-23 11:17:37

10

提供一個示例

保存蘋果

db.fruit.save({"name":"apple", "color":"red","shape":"round"}) 
WriteResult({ "nInserted" : 1 }) 

db.fruit.find(); 

{ 
    "_id" : ObjectId("53fa1809132c1f084b005cd0"), 
    "color" : "red", 
    "shape" : "round", 
    "name" : "apple" 
} 

保存的與先前保存的蘋果

db.fruit.save(
{"_id" : ObjectId("53fa1809132c1f084b005cd0"),"name":"apple", 
"color":"real red","shape":"round"}) 

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) 

現在我們保存了蘋果有_id蘋果,顏色從紅色更新爲真正的紅色

db.fruit.find(); 
{ 
    "_id" : ObjectId("53fa1809132c1f084b005cd0"), 
    "color" : "real red", 
    "shape" : "round", 
    "name" : "apple" 
} 

保存與_id

蘋果
db.fruit.save({"_id" : ObjectId("55551809132c1f084b005cd0"), 
"name":"apple", "color":"real red","shape":"round"}) 

    WriteResult({ "nMatched" : 0, "nUpserted" : 1, 
"nModified" : 0, "_id": 55551809132c1f084b005cd0 }) 

蘋果得到了插入,因爲沒有蘋果與同一對象ID做一個更新

插入一個橙色

db.fruit.insert({"name":"orange", "color":"orange","shape":"round"}) 
WriteResult({ "nInserted" : 1 }) 

橙插入

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" 
} 

所以保存,如果有對象ID供應將充當更新,提供的對象ID已經存在其他明智它插入。

49

我們這裏保存考慮兩種情況: -

1)在文檔已經_id。

2)在doc中沒有_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. 

讓我們在這裏考慮兩種情況插入: -

1),其具有收集文檔的_id。

2)沒有收集doc中的_id。

     Insert() 
        /  \ 
        /  \ 

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

Next level diagram – 2016-03-13 02:35:21

+2

Upvoted用於整齊地繪製和呈現圖表所需的時間。 – fanbondi 2016-06-10 10:52:43

0

db.<collection_name>.save(<Document>)相當於InsertOrUpdate Query。

While,db.<collection_name>.insert(<Document>)等同於插入查詢。

相關問題