update()
db.collection.update( <query>, <update>, { upsert: <boolean>, multi: <boolean>, writeConcern: <document> } )
db.collection.update( criteria, objNew, upsert, multi ) 四個參數的說明如下:
criteria: update的查詢條件,類似sql update查詢內where后面的
objNew: update的對象和一些更新的操作符(如$,$inc...)等,也可以理解為sql update查詢內set后面的
upsert: 這個參數的意思是,如果不存在update的記錄,是否插入objNew,true為插入,默認是false,不插入。
multi: mongodb默認是false,只更新找到的第一條記錄,如果這個參數為true,就把按條件查出來多條記錄全部更新。
1、$inc
修改器$inc可以對文檔的某個值為數字型(只能為滿足要求的數字)的鍵進行增減的操作。數字 型加減運算
示例文檔:{"uid":"201203","type":"1",size:10}
> db.b.insert({"uid":"201203","type":"1",size:10})
> db.b.find()
{ "_id" : ObjectId("5003b6135af21ff428dafbe6"), "uid" : "201203", "type" : "1",
"size" : 10 }
> db.b.update({"uid" : "201203"},{"$inc":{"size" : 1}})
> db.b.find()
{ "_id" : ObjectId("5003b6135af21ff428dafbe6"), "uid" : "201203", "type" : "1",
"size" : 11 }
> db.b.update({"uid" : "201203"},{"$inc":{"size" : 2}})
> db.b.find()
{ "_id" : ObjectId("5003b6135af21ff428dafbe6"), "uid" : "201203", "type" : "1",
"size" : 13 }
> db.b.update({"uid" : "201203"},{"$inc":{"size" : -1}})
> db.b.find()
{ "_id" : ObjectId("5003b6135af21ff428dafbe6"), "uid" : "201203", "type" : "1",
"size" : 12 }
2.$set
用來指定一個鍵並更新鍵值,若鍵不存在並創建新的一條數據
要在嵌入的文檔或數組中指定一個字段,請使用點符號。
db.a.findOne({"uid" : "20120002","type" : "3"})
{ "_id" : ObjectId("500216de81b954b6161a7d8f"), "desc" : "hello world2!", "num": 40, "sname" : "jk", "type" : "3", "uid" : "20120002" }
1)size鍵不存在的場合
db.a.update({"uid" : "20120002","type" : "3"},{"$set":{"size":10}})
db.a.findOne({"uid" : "20120002","type" : "3"})
{ "_id" : ObjectId("500216de81b954b6161a7d8f"), "desc" : "hello world2!", "num"
: 40, "size" : 10, "sname" : "jk", "type" : "3", "uid" : "20120002" }
2)sname鍵存在的場合
> db.a.update({"uid" : "20120002","type" : "3"},{"$set":{"sname":"ssk"}})
> db.a.find()
{ "_id" : ObjectId("500216de81b954b6161a7d8f"), "desc" : "hello world2!", "num"
: 40, "size" : 10, "sname" : "ssk", "type" : "3", "uid" : "20120002" }
{ "_id" : ObjectId("50026affdeb4fa8d154f8572"), "desc" : "hello world1!", "num"
: 50, "sname" : "jk", "type" : "1", "uid" : "20120002" }
3)可改變鍵的值類型
> db.a.update({"uid" : "20120002","type" : "3"},{"$set":{"sname":["java",".net","c++"]}})
db.a.findOne({"uid" : "20120002","type" : "3"})
{
"_id" : ObjectId("500216de81b954b6161a7d8f"),
"desc" : "hello world2!",
"num" : 40,
"size" : 10,
"sname" : [
"java",
".net",
"c++"
],
"type" : "3",
"uid" : "20120002"
}
4)對於內嵌的文檔,$set又是如何進行更新的內嵌的文檔的呢,請看下面的示例:
示例文檔:{"name":"toyota","type":"suv","size":{"height":10,"width":5,"length":15}}
> db.c.findOne({"name":"toyota"})
{
"_id" : ObjectId("5003be465af21ff428dafbe7"),
"name" : "toyota",
"type" : "suv",
"size" : {
"height" : 10,
"width" : 5,
"length" : 15
}
}
db.c.update({"name":"toyota"},{"$set":{"size.height":8}})
db.c.findOne({"name":"toyota"})
{
"_id" : ObjectId("5003be465af21ff428dafbe7"),
"name" : "toyota",
"type" : "suv",
"size" : {
"height" : 8,
"width" : 5,
"length" : 15
}
}
db.c.update({"name":"toyota"},{"$set":{"size.width":7}})
> db.c.findOne({"name":"toyota"})
{
"_id" : ObjectId("5003be465af21ff428dafbe7"),
"name" : "toyota",
"type" : "suv",
"size" : {
"height" : 8,
"width" : 7,
"length" : 15
}
}
可見:對於內嵌文檔在使用$set更新時,使用"."連接的方式。
3.$unset
$unset操作符會刪除一個特定的字段
要在嵌入的文檔或數組中指定一個字段,請使用點符號。
如果該字段不存在,則$unset不做任何操作(即不操作)。
當使用$匹配一個數組元素時,$unset將匹配的元素替換為null,而不是從數組中刪除匹配的元素。這種行為保持了數組大小和元素位置的一致性。
db.user.update({"email_state":{"$exists":true}},{"$unset":{"email_state",""}},{multi:true});
刪除user表的email_state字段。
模版:
db.表.update({"field1":{"$exists":true}},{"$unset":{"field1",""}},{multi:true})
$exists:判斷存在該字段。
注意在后面需要加上multi:true,刪除多行
得出結論:使用修改器$unset時,不論對目標鍵使用1、0、-1或者具體的字符串等都是可以刪除該目標鍵。
db.a.update({"uid" : "20120002","type" : "3"},{"$unset":{"sname":1}})
db.a.findOne({"uid" : "20120002","type" : "3"})
{
"_id" : ObjectId("500216de81b954b6161a7d8f"),
"desc" : "hello world2!",
"num" : 40,
"size" : 10,
"type" : "3",
"uid" : "20120002"
}
db.a.update({"uid" : "20120002","type" : "3"},{"$unset":{"num":0}})
db.a.findOne({"uid" : "20120002","type" : "3"})
{
"_id" : ObjectId("500216de81b954b6161a7d8f"),
"desc" : "hello world2!",
"size" : 10,
"type" : "3",
"uid" : "20120002"
}
db.a.update({"uid" : "20120002","type" : "3"},{"$unset":{"size":-1}})
db.a.findOne({"uid" : "20120002","type" : "3"})
{
"_id" : ObjectId("500216de81b954b6161a7d8f"),
"desc" : "hello world2!",
"type" : "3",
"uid" : "20120002"
}
db.a.update({"uid" : "20120002","type" : "3"},{"$unset":{"desc":"sssssss"}})
db.a.findOne({"uid" : "20120002","type" : "3"})
{
"_id" : ObjectId("500216de81b954b6161a7d8f"),
"type" : "3",
"uid" : "20120002"
}
4、數組修改器--$push
$push--向文檔的某個數組類型的鍵添加一個數組元素,不過濾重復的數據。添加時鍵存在,要求鍵值類型必須是數組;鍵不存在,則創建數組類型的鍵。
> db.c.find()
{ "_id" : ObjectId("5003be465af21ff428dafbe7"), "name" : "toyota", "type" : "suv",
"size" : { "height" : 8, "width" : 7, "length" : 15 } }
先push一個當前文檔中不存在的鍵title
> db.c.update({"name" : "toyota"},{$push:{"title":"t1"}})
> db.c.find()
{ "_id" : ObjectId("5003be465af21ff428dafbe7"), "name" : "toyota", "size" : { "height" : 8,
"width" : 7, "length" : 15 }, "title" : [ "t1" ], "type" : "suv" }
--再向title中push一個值
> db.c.update({"name" : "toyota"},{$push:{"title":"t2"}})
> db.c.find()
{ "_id" : ObjectId("5003be465af21ff428dafbe7"), "name" : "toyota", "size" : { "height" : 8,
"width" : 7, "length" : 15 }, "title" : [ "t1", "t2" ], "type" : "suv" }
--再向title中push一個值
> db.c.update({"name" : "toyota"},{$push:{"title":"t2"}})
> db.c.find()
{ "_id" : ObjectId("5003be465af21ff428dafbe7"), "name" : "toyota", "size" : { "height" : 8,
"width" : 7, "length" : 15 }, "title" : [ "t1", "t2", "t2" ], "type" : "suv" }
--再向一個已經存在的鍵值非數組類型的鍵push一個值
> db.c.update({"name" : "toyota"},{$push:{"size.height":10}})
Cannot apply $push/$pushAll modifier to non-array
> db.c.update({"name" : "toyota"},{$push:{"name":"ddddddd"}})
Cannot apply $push/$pushAll modifier to non-array
5.數組修改器--$ne/$addToSet
---------------------------------------------------------------------
$addToSet操作符在數組中添加一個值,除非該值已經存在,否則$addToSet對該數組沒有任何作用。
$addToSet只會確保在集合中沒有重復的條目,並且不會影響現有的重復元素。$addToSet並不保證在修改后的集合中元素的特定順序。
如果該字段不是數組,則操作將失敗。
主要給數組類型鍵值添加一個元素時,避免在數組中產生重復數據,$ne在有些情況是不通行的。
1)單個添加到數組
> db.c.update({"title" : {$ne:"t2"}},{$push:{"title":"t2"}})
> db.c.find()
{ "_id" : ObjectId("5003be465af21ff428dafbe7"), "name" : "toyota", "size" : { "height" : 8,
"width" : 7, "length" : 15 }, "title" : [ "t1", "t2", "t2" ], "type" : "suv" }
> db.c.update({"name" : "toyota"},{$addToSet:{"title":"t2"}})
> db.c.find()
{ "_id" : ObjectId("5003be465af21ff428dafbe7"), "name" : "toyota", "size" : { "height" : 8,
"width" : 7, "length" : 15 }, "title" : [ "t1", "t2", "t2" ], "type" : "suv" }
2)包含數組字段字母的集合測試中的文檔:
{ _id: 1, letters: ["a", "b"] }
db.test.update( { _id: 1 }, { $addToSet: {letters: [ "c", "d" ] } } )
{ _id: 1, letters: [ "a", "b", [ "c", "d" ] ] }
3)添加多個用$each:與$push和$addToSet結合,一次給數組添加多個值。
{ _id: 2, item: "cable", tags: [ "electronics", "supplies" ] }
db.inventory.update( { _id: 2 }, { $addToSet: { tags: { $each: [ "camera", "electronics", "accessories" ] } } } )
db.person.update({"name":"ryan"},{"$push":{"language":{"$each":["Japanese","Portuguese"]}}},true,true);
db.person.update({"name":"ryan"},{"$addToSet":{"language":{"$each":["Japanese","Portuguese"]}}},true,true);
添加的electronics已有,結果如下。
{ _id: 2, item: "cable", tags: [ "electronics", "supplies", "camera", "accessories" ] }
4)、修改
操作符:$set修改數組中的元素
例句:db.blog.update({"comments.testAdd":"T"},{$set:{"comments.$.testAdd":"z"}});
這里注意第一個查詢條件必須數組.字段名,否則修改失敗,有人可能會問后面的{"comments.$.testAdd":"z"}中的$符是干嘛用的,他在這里面代表的相當於是數組的下表,如果我們明確的知道下標的話,我們完全可以這么寫,比如下表為0:{"comments.0.testAdd":"z"},但大多數情況下我們是不知道下標的,所以用通配符$來表示,這樣只會修改匹配的第一條數據,而不是所有匹配到的數據,這點需要注意
"tags":["Python","MongoDB"]
修改數組滿足條件內容修改
這個將先搜索tags中滿足”MongoDB”的,如果找到,就把它修改為”Hello”。可以看到上面的update這個函數已經有兩個參數了,它還有第3個參數upsert,如果設為”True”,則如果沒有找到匹配的文檔,就會在匹配的基礎上新建一個文檔,具體實例就不講了。
posts.update({"tags":"MongoDB"},{"$set":{"tags.$":"Hello"}})
6、數組修改器--$pop、$pull
db.collection.update( { field: <query> }, { $pull: { field: <query> } } );
當數組中的值為字符串或數字時,用pull或pullAll很方便,field后面的值直接跟上數組中的值即可。如果數組中的值為對象時,其實也很簡單,field后跟上對象中條件字段和值即可。
$pop從數組的頭或者尾刪除數組中的元素,示例如下:
{ "_id" : ObjectId("5003be465af21ff428dafbe7"), "name" : "toyota", "size" : { "height" : 8,
"width" : 7, "length" : 15 }, "title" : [ "t1", "t2", "t3", "t4" ],"type" : "suv" }
--從數組的尾部刪除 1
> db.c.update({"name" : "toyota"},{$pop:{"title":1}})
> db.c.find()
{ "_id" : ObjectId("5003be465af21ff428dafbe7"), "name" : "toyota", "size" : { "height" : 8,
"width" : 7, "length" : 15 }, "title" : [ "t1", "t2", "t3" ], "type" : "suv" }
--從數組的頭部 -1
> db.c.update({"name" : "toyota"},{$pop:{"title":-1}})
> db.c.find()
{ "_id" : ObjectId("5003be465af21ff428dafbe7"), "name" : "toyota", "size" : { "height" : 8,
"width" : 7, "length" : 15 }, "title" : [ "t2", "t3" ], "type" : "suv" }
--從數組的尾部刪除 0
> db.c.update({"name" : "toyota"},{$pop:{"title":0}})
> db.c.find()
{ "_id" : ObjectId("5003be465af21ff428dafbe7"), "name" : "toyota", "size" : { "height" : 8,
"width" : 7, "length" : 15 }, "title" : [ "t2" ], "type" : "suv" }
$pull從數組中刪除滿足條件的元素
{ "_id" : ObjectId("5003be465af21ff428dafbe7"), "name" : "toyota", "size" : { "height" : 8,
"width" : 7, "length" : 15 }, "title" : [ "t1", "t2", "t2", "t3" ],"type" : "suv" }
> db.c.update({"name" : "toyota"},{$pull:{"title":"t2"}})
> db.c.find()
{ "_id" : ObjectId("5003be465af21ff428dafbe7"), "name" : "toyota", "size" : { "height" : 8,
"width" : 7, "length" : 15 }, "title" : [ "t1", "t3" ], "type" : "suv" }
db.test.find({"msgid":170}).toArray()
[
{
"_id" : ObjectId("52fd7abe5cf0fb00ee4775bf"),
"msgid" : NumberLong(170),
"msg" : [
{
"comcont" : "heddhefasdfa",
"comtime" : NumberLong(1392346547)
},
{
"comcont" : "heddhefasdfa",
"comtime" : NumberLong(1392346667)
}
]
}
]
刪除comtime為1392346547的對象。,如果數組里兩個條件就寫兩個條件就可以
db.test.update({"msgid":170},{"$pull":{"msg":{"comtime":1392346547}}})
db.test.find({"msgid":170}).toArray()
[
{
"_id" : ObjectId("52fd7abe5cf0fb00ee4775bf"),
"msgid" : NumberLong(170),
"msg" : [
{
"comcont" : "heddhefasdfa",
"comtime" : NumberLong(1392346667)
}
]
}
]
數組的定位修改器
在需要對數組中的值進行操作的時候,可通過位置或者定位操作符("$").數組是0開始的,可以直接將下標作為鍵來選擇元素。
{"uid":"001",comments:[{"name":"t1","size":10},{"name":"t2","size":12}]}
> db.c.find({"uid":"001"})
{ "_id" : ObjectId("5003da405af21ff428dafbe8"), "uid" : "001", "comments" : [ {
"name" : "t1", "size" : 10 }, { "name" : "t2", "size" : 12 } ] }
> db.c.update({"uid":"001"},{$inc:{"comments.0.size":1}})
> db.c.find({"uid":"001"})
{ "_id" : ObjectId("5003da405af21ff428dafbe8"), "uid" : "001", "comments" : [ {
"name" : "t1", "size" : 11 }, { "name" : "t2", "size" : 12 } ] }
> db.c.update({"comments.name":"t1"},{$set:{"comments.$.size":1}})
> db.c.find({"uid":"001"})
{ "_id" : ObjectId("5003da405af21ff428dafbe8"), "uid" : "001", "comments" : [ {
"name" : "t1", "size" : 1 }, { "name" : "t2", "size" : 12 } ] }
--若為多個文檔滿足條件,則只更新第一個文檔。
$setOnInsert更新或插入
當該key不存在的時候執行插入操作,當存在的時候則不管,可以使用setOnInsert
db.wyg.update({'_id': 'id'}, {'$setOnInsert': {'a': 'a'}, '$set': {'b': 'b'}}, true)
當id存在的時候,忽略setOnInsert。
當id存在的時候,如果要插入,則插入{'a': 'a'}
最后的參數true,則是指明,當update不存在的_id時,執行插入操作。默認是false,只更新,不插入。
push、setOnInsert:db.cswuyg.update({"_id": "abc"}, {$push: {"name": "c"}, $setOnInsert: {"cc":"xx"}}, true)
upsert
upsert是一種特殊的更新。當沒有符合條件的文檔,就以這個條件和更新文檔為基礎創建一個新的文檔,如果找到匹配的文檔就正常的更新。
使用upsert,既可以避免競態問題,也可以減少代碼量(update的第三個參數就表示這個upsert,參數為true時)
> db.c.remove()
> db.c.update({"size":11},{$inc:{"size":3}})
> db.c.find()
> db.c.update({"size":11},{$inc:{"size":3}},false)
> db.c.find()
> db.c.update({"size":11},{$inc:{"size":3}},true)
> db.c.find()
{ "_id" : ObjectId("5003ded6c28f67507a6df1de"), "size" : 14 }
save函數
1.可以在文檔不存在的時候插入,存在的時候更新,只有一個參數文檔。
2.要是文檔含有"_id",會調用upsert。否則,會調用插入。
> db.a.find()
{ "_id" : ObjectId("50026affdeb4fa8d154f8572"), "desc" : "hello world1!", "num": 50,
"sname" : "jk", "type" : "1", "uid" : "20120002" }
> var o = db.a.findOne()
> o.num = 55
55
> db.a.save(o)
> db.a.find()
{ "_id" : ObjectId("50026affdeb4fa8d154f8572"), "desc" : "hello world1!", "num": 55,
"sname" : "jk", "type" : "1", "uid" : "20120002" }