修改数据:
update() 方法用于更新已存在的文档。语法格式如下:
db.collection.update(
<query>,
<update>,
{
upsert: <boolean>,
multi: <boolean>,
writeConcern: <document>
}
)
参数说明:
query : update的查询条件,类似sql update查询内where后面的。
update : update的对象和一些更新的操作符(如$,$inc…)等,也可以理解为sql update查询内set后面的
upsert : 可选,这个参数的意思是,如果不存在update的记录,是否插入objNew,true为插入,默认是false,不插入。
multi : 可选,mongodb 默认是false,只更新找到的第一条记录,如果这个参数为true,就把按条件查出来多条记录全部更新。
writeConcern:可选,抛出异常的级别。
如果需要修改指定的属性,而不是替换需要使用“修改操作符”来完成修改
update()默认只会修改一个
语法:db.集合名称.update({查询条件},{修改条件})
updateOne() 修改匹配的第一条
updateMany() 修改匹配所有条
1.有$set的修改
update updateOne
$set 可以用来修改文档中的指定属性 (修改指定属性的值;如果属性不存在则新增该属性)
db.skd.update({"major":"历史"},{$set:{"major":"考古系"}})
{
"_id" : "668f528f301d1375d03a07ed",
"name" : "wqz",
"age" : 23,
"gender" : "男",
"hobby" : [
"football",
"running",
"history"
],
"major" : "考古系"
}
db.skd.updateOne({"major":"考古系"},{$set:{"major":"历史系"}})
{
"_id" : "668f528f301d1375d03a07ed",
"name" : "wqz",
"age" : 23,
"gender" : "男",
"hobby" : [
"football",
"running",
"history"
],
"major" : "历史系"
}
db.skd.update({"name":"shc"},{$set:{"name":"sqz",'age':17}})
{
"_id" : "668f528f301d1375d03a07ee",
"name" : "sqz",
"age" : 17,
"gender" : "女",
"hobby" : [
"running",
"music",
"drawing"
],
"major" : "医学"
}
2.$unset 可以用来删除文档的指定属性,
db.skd.update({"name":"sqz"},{$unset:{'age':17}})
{
"_id" : "668f528f301d1375d03a07ee",
"name" : "sqz",
"gender" : "女",
"hobby" : [
"running",
"music",
"drawing"
],
"major" : "医学"
}
3.$inc 用法:{$inc:{field:value}}
作用:对一个数字字段的某一个field增加value
db.skd.update({"name":"sqz"},{$inc:{"age":1}})
//对“sqz”的年龄加1岁
{
...
"major" : "医学",
"age" : 18
}
4.$push 用法:{$push:{field:value}}
作用:把value追加到field里,注:field只能是数组类型,如果field不存在,会自动插入一个数组类型
示例:
db.skd.update({"name":"sqz"},{$push:{"alias":["A","B"]}})
{
"_id" : "668f528f301d1375d03a07ee",
"name" : "sqz",
"gender" : "女",
"hobby" : [
"running",
"music",
"drawing"
],
"major" : "医学",
"age" : 18,
"alias" : [
[
"A",
"B"
]
]
}
//删掉alias字段
db.skd.update({“name”:”sqz”},{$unset:{“alias”:[“A”,”B”]}})
5.$addToSet 用法:{$addToSet:{field:value}}
作用:加一个值到数组内,而且只有当这个值在数组中不存在时才增加
db.skd.update({"name":"sqz"},{$addToSet:{"alias":["C","D"]}})
{
"_id" : "668f528f301d1375d03a07ee",
"name" : "sqz",
"gender" : "女",
"hobby" : [
"running",
"music",
"drawing"
],
"major" : "医学",
"age" : 18,
"alias" : [
[
"A",
"B"
],
[
"C",
"D"
]
]
}
[“A”,”B”],[“C”,”D”]
添加一个数据是”C”,”D”的数组对象,不是将两个数据一次加入alias数组。
继续用$push添加会怎么样呢?
db.skd.update({"name":"sqz"},{$push:{"alias":["E","F"]}})
{
...
"age" : 18,
"alias" : [
[
"A",
"B"
],
[
"C",
"D"
],
[
"E",
"F"
]
]
}
对mongo的不熟悉暂时有个疑问$push添加了[“A”,”B”]数组, $addToSet 添加了[“C”,”D”], 继续$push添加了[“E”,”F”],那么 $addToSet和$push区别是啥呢?应该这样写:
db.skd.update({“name”:”sqz”},{$push:{“alias”:”A”}})
db.skd.update({“name”:”sqz”},{$push:{“alias”:”B”}})
db.skd.update({“name”:”sqz”},{$push:{“alias”:”C”}})
6.$pop
用法:删除数组内第一个值:{$pop:{field:-1}};
删除数组内最后一个值:{$pop:{field:1}}
db.skd.update({"name":"sqz"},{$pop:{"alias":-1}})
{
"_id" : "668f528f301d1375d03a07ee",
"name" : "sqz",
"gender" : "女",
"hobby" : [
"running",
"music",
"drawing"
],
"major" : "医学",
"age" : 18,
"alias" : [
[
"C",
"D"
],
[
"E",
"F"
]
]
}
删除了[“A”,”B”]的值;
db.skd.update({"name":"sqz"},{$pop:{"alias":1}})
{
"_id" : "668f528f301d1375d03a07ee",
"name" : "sqz",
"gender" : "女",
"hobby" : [
"running",
"music",
"drawing"
],
"major" : "医学",
"age" : 18,
"alias" : [
[
"C",
"D"
]
]
}
删除了最后的[“E”,”F”]值;
7.$pull 用法:{$pull:{field:_value}}
作用:从数组field内删除一个等于_value的值
db.skd.update({“name”:”sqz”},{$push:{“alias”:[“A”,”B”]}})
db.skd.update({“name”:”sqz”},{$push:{“alias”:[“E”,”F”]}})
db.skd.update({“name”:”sqz”},{$push:{“alias”:[“H”]}})
先把A,B,E,F还原回去;
db.skd.update({"name":"sqz"},{$pull:{"alias":["H"]}})
db.skd.update({"name":"sqz"},{$pull:{"alias":["A","B"]}})
{
"_id" : "668f528f301d1375d03a07ee",
"name" : "sqz",
"gender" : "女",
"hobby" : [
"running",
"music",
"drawing"
],
"major" : "医学",
"age" : 18,
"alias" : [
[
"C",
"D"
],
[
"E",
"F"
]
]
}
8.$pullAll 用法:{$pullAll:value:_array}
作用:同$pull一样,可以一次性删除数组内的多个值
db.skd.update({"name":"sqz"},{$push:{"alias":"A"}})
db.skd.update({"name":"sqz"},{$push:{"alias":"B"}})
db.skd.update({"name":"sqz"},{$push:{"alias":"C"}})
db.skd.update({"name":"sqz"},{$push:{"alias":"H"}})
db.skd.update({"name":"sqz"},{$pullAll:{"alias":["A","B","C"]}})
{
"_id" : "668f528f301d1375d03a07ee",
"name" : "sqz",
"gender" : "女",
"hobby" : [
"running",
"music",
"drawing"
],
"major" : "医学",
"age" : 17,
"alias" : [
"H"
]
}
9.$rename 用法:{$rename:{old:field_name:new_field_name}}
作用:对字段进行重命名:
db.skd.update({"name":"sqz"},{$rename:{"alias":"code"}})
{
"_id" : "668f528f301d1375d03a07ee",
"name" : "sqz",
"gender" : "女",
"hobby" : [
"running",
"music",
"drawing"
],
"major" : "医学",
"age" : 17,
"code" : [
"H"
]
}