MongoDB——更新操作符

MongoDB的更新操作符

1.$inc

> {$inc:{field:value}}
  • 作用:对一个数字字段的某个field增加value
  • 示例:将name为newzhong的学生的age增加5
> db.students.find()
{ "_id" : ObjectId("5030f7ac721e16c4ab180cdb"), "name" : "newzhong", "age" : 22 }
#查询结果显示年龄为22
> db.students.update({name:"newzhong"},{$inc:{age:5}})
#执行修改,把age增加5
> db.students.find()
{ "_id" : ObjectId("5030f7ac721e16c4ab180cdb"), "name" : "newzhong", "age" : 27 }
> 
#查询结果显示年龄为27,修改成功

2.$set

用法:{$set:{field:value}}

  • 作用:把文档中某个字段field的值设为value
  • 示例: 把newzhong的年龄设为23岁
> db.students.find()
{ "_id" : ObjectId("5030f7ac721e16c4ab180cdb"), "name" : "newzhong", "age" : 27 }
> db.students.update({name:"newzhong"},{$set:{age:23}})
> db.students.find()
{ "_id" : ObjectId("5030f7ac721e16c4ab180cdb"), "name" : "newzhong", "age" : 23 }
>

3. $unset

用法:

{$unset:{field:1}}
  • 作用:删除某个字段field
  • 示例: 将newzhong的年龄字段删除
> db.students.find()
{ "_id" : ObjectId("5030f7ac721e16c4ab180cdb"), "name" : "newzhong", "age" : 23 }
> db.students.update({name:"newzhong"},{$unset:{age:1}})
> db.students.find()
{ "_id" : ObjectId("5030f7ac721e16c4ab180cdb"), "name" : "newzhong }
>

4.$push

用法:{$push:{field:value}}

  • 作用:把value追加到field里。注:field只能是数组类型,如果field不存在,会自动插入一个数组类型
  • 示例:给newzhong添加别名"michael"
> db.students.find()
{ "_id" : ObjectId("5030f7ac721e16c4ab180cdb"), "name" : "newzhong" }
> db.students.update({name:"newzhong"},{$push:{"ailas":"Michael"}})
> db.students.find()
{ "_id" : ObjectId("5030f7ac721e16c4ab180cdb"), "ailas" : [ "Michael" ], "name" : "newzhong" }
>

5.pushAll

用法{$pushAll:{field:value_array}}

  • 作用:用法同 p u s h 一 样 , 只 是 push一样,只是 pushpushAll可以一次追加多个值到一个数组字段内。
  • 示例:给newzhong追加别名A1,A2
> db.students.find()
{ "_id" : ObjectId("5030f7ac721e16c4ab180cdb"), "ailas" : [ "Michael" ], "name" : "newzhong" }
> db.students.update({name:"newzhong"},{$pushAll:{"ailas":["A1","A2"]}})
> db.students.find()
{ "_id" : ObjectId("5030f7ac721e16c4ab180cdb"), "ailas" : [ "Michael", "A1", "A2" ], "name" : "newzhong" }
>

6.$addToSet

用法:{$addToSet:{field:value}}

  • 作用:加一个值到数组内,而且只有当这个值在数组中不存在时才增加。
  • 示例:往newzhong的别名字段里添加两个别名A3、A4
> db.students.find()
{ "_id" : ObjectId("5030f7ac721e16c4ab180cdb"), "ailas" : [ "Michael", "A1", "A2" ], "name" : "newzhong" }
> db.students.update({name:"newzhong"},{$addToSet:{"ailas":["A3","A4"]}})
> db.students.find()
{ "_id" : ObjectId("5030f7ac721e16c4ab180cdb"), "ailas" : [ "Michael", "A1", "A2", [ "A3", "A4" ] ], "name" : "newzhong" }
>

7.$pop

用法:删除数组内第一个值:{KaTeX parse error: Expected 'EOF', got '}' at position 15: pop:{field:-1}}̲、删除数组内最后一个值:{pop:{field:1} }

  • 作用:用于删除数组内的一个值
  • 示例: 删除newzhong记录中alias字段中第一个别名
> db.students.find()
{ "_id" : ObjectId("5030f7ac721e16c4ab180cdb"), "ailas" : [ "Michael", "A1", "A2", [ "A3", "A4" ] ], "name" : "newzhong" }
> db.students.update({name:"newzhong"},{$pop:{"ailas":-1}})
> db.students.find()
{ "_id" : ObjectId("5030f7ac721e16c4ab180cdb"), "ailas" : [ "A1", "A2", [ "A3", "A4" ] ], "name" : "newzhong" }
>

8.$pull

用法:{$pull:{field:_value}}

  • 作用:从数组field内删除一个等于_value的值
  • 示例: 删除newzhong记录中的别名A1
> db.students.find()
{ "_id" : ObjectId("5030f7ac721e16c4ab180cdb"), "ailas" : [ "A1", "A2" ], "name" : "newzhong" }
> db.students.update({name:"newzhong"},{$pull:{"ailas":"A1"}})
> db.students.find()
{ "_id" : ObjectId("5030f7ac721e16c4ab180cdb"), "ailas" : [ "A2" ], "name" : "newzhong" }
>

9.$pullAll

用法{$pullAll:value_array}

  • 作用:用法同$pull一样,可以一次性删除数组内的多个值。
  • 示例: 删除newzhong记录内的所有别名
> db.students.find()
{ "_id" : ObjectId("5030f7ac721e16c4ab180cdb"), "ailas" : [ "A1", "A2" ], "name" : "newzhong" }
> db.students.update({name:"newzhong"},{$pullAll:{"ailas":["A1","A2"]}})
> db.students.find()
{ "_id" : ObjectId("5030f7ac721e16c4ab180cdb"), "ailas" : [ ], "name" : "newzhong" }
>

10.$rename

用法:{$rename:{old_field_name:new_field_name}}

  • 作用:对字段进行重命名
  • 示例:把newzhong记录的name字段重命名为sname
> db.students.find()
{ "_id" : ObjectId("5030f7ac721e16c4ab180cdb"), "ailas" : [ ], "name" : "newzhong" }
> db.students.update({name:"newzhong"},{$rename:{"name":"sname"}})
> db.students.find()
{ "_id" : ObjectId("5030f7ac721e16c4ab180cdb"), "ailas" : [ ], "sname" : "newzhong" }
>

你可能感兴趣的:(数据库)