字段更新操作符
1. $set:用来指定一个键的值。如果这个键不存在,则创建它。
添加例子:
首先添加一条数据:
db.posts.insert({"name":"sean","age":33,"sex":"male"})
> db.posts.find({"name":"sean"})
{ "_id" : ObjectId("59a6df85a95c5eb6f7267c00"), "name" : "sean", "age" : 33, "sex" : "male" }
执行更新语句:
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属性,因为源数据中没有改属性则新增。
mongodb中的更新操作和关系数据库中更新不同,mongodb更新的属性值可以与源属性值不同类型的数据。可以更新为数组、文档、不同类型数据等等。
> 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:从文档中移除指定属性
执行语句之后发现hobby属性已经被移除。
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 ] }