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 ] }
相关文章
相关标签/搜索