Mongodb語法學習:更新

字段更新操做符redis

1. $set:用來指定一個鍵的值。若是這個鍵不存在,則建立它。mongodb

添加例子:數據庫

首先添加一條數據:數組

db.posts.insert({"name":"sean","age":33,"sex":"male"})
> db.posts.find({"name":"sean"})
{ "_id" : ObjectId("59a6df85a95c5eb6f7267c00"), "name" : "sean", "age" : 33, "sex" : "male" }

執行更新語句:
 post

db.posts.update({"_id":ObjectId("59a6df85a95c5eb6f7267c00")},{"$set":{"hobby":"reading"}})
> db.posts.find({"_id":ObjectId("59a6df85a95c5eb6f7267c00")})
{ "_id" : ObjectId("59a6df85a95c5eb6f7267c00"), "name" : "sean", "age" : 33, "sex" : "male", "hobby" : "reading" }

經過查詢咱們發現已經添加成功。在update語句中咱們發現有兩組參數,第一組至關於查詢條件及要更新的數據必須知足的條件,第二組數據是咱們的操做更新hobby屬性,由於源數據中沒有改屬性則新增。code

mongodb中的更新操做和關係數據庫中更新不一樣,mongodb更新的屬性值能夠與源屬性值不一樣類型的數據。能夠更新爲數組、文檔、不一樣類型數據等等。ci

> db.posts.update({"_id" : ObjectId("59a6df85a95c5eb6f7267c00")},{"$set":{"sex":1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.posts.find({"_id":ObjectId("59a6df85a95c5eb6f7267c00")})
{ "_id" : ObjectId("59a6df85a95c5eb6f7267c00"), "name" : "sean", "age" : 33, "sex" : 1, "hobby" : "reading" }
> db.posts.update({"_id":ObjectId("59a6df85a95c5eb6f7267c00")},{"$set":{"hobby":["reading","runing","playing"]}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.posts.find({"_id":ObjectId("59a6df85a95c5eb6f7267c00")});
{ "_id" : ObjectId("59a6df85a95c5eb6f7267c00"), "name" : "sean", "age" : 33, "sex" : 1, "hobby" : [ "reading", "runing", "playing" ] }

經過update語句還能夠更新(沒有時添加)內嵌文檔屬性:文檔

db.posts.update({"_id":ObjectId("59a6df85a95c5eb6f7267c00")},{"$set":{"author.name":"sean","author.age":22}})
db.posts.find({"_id":ObjectId("59a6df85a95c5eb6f7267c00")}).pretty()
{
	"_id" : ObjectId("59a6df85a95c5eb6f7267c00"),
	"name" : "sean",
	"age" : 33,
	"sex" : 1,
	"hobby" : [
		"reading",
		"runing",
		"playing"
	],
	"author" : {
		"name" : "sean",
		"age" : 22
	}
}

2. $unset:從文檔中移除指定屬性it

執行語句以後發現hobby屬性已經被移除。date

db.posts.update({"_id":ObjectId("59a6df85a95c5eb6f7267c00")},{"$unset":{"hobby":0}})
> db.posts.find({"_id":ObjectId("59a6df85a95c5eb6f7267c00")}).pretty()
{
	"_id" : ObjectId("59a6df85a95c5eb6f7267c00"),
	"name" : "sean",
	"age" : 33,
	"sex" : 1,
	"author" : {
		"name" : "sean",
		"age" : 22
	}
}

3. $inc :修改器用來增長已有鍵的值,或者在鍵不存在時建立一個鍵。inc"修改器用來增長已有鍵的值,或者在鍵不存在時建立一個鍵。inc就是專門來增長(和減小)數字的。"$inc"只能用於整數、長整數或雙精度浮點數。要是用在其餘類型的數據上就會致使操做失敗。

以下面例子:咱們給前面添加數據的做者增長一歲

db.posts.update({"_id" : ObjectId("59a6df85a95c5eb6f7267c00")},{"$inc":{"author.age":1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.posts.find({"_id" : ObjectId("59a6df85a95c5eb6f7267c00")}).pretty()
{
	"_id" : ObjectId("59a6df85a95c5eb6f7267c00"),
	"name" : "sean",
	"age" : 33,
	"sex" : 1,
	"author" : {
		"name" : "sean",
		"age" : 23
	}
}

4. $rename

語法: {$rename: { <old name1>: <new name1>, <old name2>: <new name2>, ... } }

$rename操做符能夠重命名字段名稱,新的字段名稱不能和文檔中現有的字段名相同。若是文檔中存在A、B字段,將B字段重命名爲A,$rename會將A字段和值移除掉,而後將B字段名改成A。

下面例子,既能夠修改文檔屬性名也能夠修改子文檔屬性名

> db.posts.update({"_id" : ObjectId("59a6df85a95c5eb6f7267c00")},{"$rename":{"age":"num","author.age":"author.num"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.posts.find({"_id" : ObjectId("59a6df85a95c5eb6f7267c00")}).pretty()
{
	"_id" : ObjectId("59a6df85a95c5eb6f7267c00"),
	"name" : "sean",
	"sex" : 1,
	"author" : {
		"name" : "sean",
		"num" : 23
	},
	"num" : 33
}

$rename也能夠將子文檔A的屬性移到子文檔B的中

db.posts.update({"_id" : ObjectId("59a6df85a95c5eb6f7267c00")},{"$rename":{"author.num":"house.num"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.posts.find({"_id" : ObjectId("59a6df85a95c5eb6f7267c00")}).pretty()
{
	"_id" : ObjectId("59a6df85a95c5eb6f7267c00"),
	"name" : "sean",
	"sex" : 1,
	"author" : {
		"name" : "sean"
	},
	"num" : 33,
	"house" : {
		"num" : 23
	}
}

5. upsert :upset不是一個操做符,他是update的第三個參數,是一個booleen類型,默認爲false。當咱們賦值爲true時,就表示,若是更新的數據不存在咱們就按照條件建立一個,而後再進行更新操做。

> db.posts.update({"name":"gaoqiumin"},{"$set":{"age":30}},true)
WriteResult({
	"nMatched" : 0,
	"nUpserted" : 1,
	"nModified" : 0,
	"_id" : ObjectId("59a6ee7d6441b73551275b5f")
})
> db.posts.find({"name":"gaoqiumin"}).pretty()
{
	"_id" : ObjectId("59a6ee7d6441b73551275b5f"),
	"name" : "gaoqiumin",
	"age" : 30
}

更新的值不存在,而後咱們新建了該文檔,而後進行更新。

6. $setOnInsert:操做符,當upsert爲true時,該操做符爲新建立的文檔添加屬性。

> db.posts.update({"name":"wangxiaofang"},{"$setOnInsert":{"age":33,"com":"shanghah"}},true)
WriteResult({
	"nMatched" : 0,
	"nUpserted" : 1,
	"nModified" : 0,
	"_id" : ObjectId("59a6efb26441b73551275b78")
})
> db.posts.find({"name":"wangxiaofang"}).pretty()
{
	"_id" : ObjectId("59a6efb26441b73551275b78"),
	"name" : "wangxiaofang",
	"age" : 33,
	"com" : "shanghah"
}

當條件查詢的文檔已經存在就不會添加$setOnInsert操做符的內容,且不影響$set操做符操做。

> db.posts.update({"name":"wangxiaofang"},{"$setOnInsert":{"city":"zhengzhou"},"$set":{"age":25}},true)
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.posts.find({"name":"wangxiaofang"}).pretty()
{
	"_id" : ObjectId("59a6efb26441b73551275b78"),
	"name" : "wangxiaofang",
	"age" : 25,
	"com" : "shanghah"
}

7. $push:向已有的數組末尾添加元素

db.posts.find({"title":"MongoDB"}).pretty()
{
	"_id" : ObjectId("59a6f2576441b73551275bb6"),
	"title" : "MongoDB",
	"comments" : [
		{
			"name" : "egger",
			"content" : "thks!"
		}
	]
}
> db.posts.update({"title":"MongoDB"},{$push:{"comments":{"name":"redis","content":"doit"}}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.posts.find({"title":"MongoDB"}).pretty()
{
	"_id" : ObjectId("59a6f2576441b73551275bb6"),
	"title" : "MongoDB",
	"comments" : [
		{
			"name" : "egger",
			"content" : "thks!"
		},
		{
			"name" : "redis",
			"content" : "doit"
		}
	]
}

8. $pull :移除數組中匹配規則的值

db.profiles.insert({ votes: [ 3, 5, 6, 7, 7, 8 ] });
//移除數組中全部元素7
db.profiles.update( { votes: 3 }, { $pull: { votes: 7 } } );
//Result
{ votes: [ 3, 5, 6, 8 ] }

//移除數組中全部大於6的元素
db.profiles.update( { votes: 3 }, { $pull: { votes: { $gt: 6 } } } );
{ votes: [ 3, 5, 6 ] }
相關文章
相關標籤/搜索