> {$inc:{field:value}}
> 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,修改成功
用法:
{$set:{field:value}}
> 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 }
>
用法:
{$unset:{field:1}}
> 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 }
>
用法:
{$push:{field:value}}
> 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" }
>
用法:
{$pushAll:{field:value_array}}
> 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" }
>
用法:{$addToSet:{field:value}}
> 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" }
>
用法:删除数组内第一个值:{KaTeX parse error: Expected 'EOF', got '}' at position 15: pop:{field:-1}}̲、删除数组内最后一个值:{pop:{field:1} }
> 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" }
>
用法:{$pull:{field:_value}}
> 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" }
>
用法:
{$pullAll:value_array}
> 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" }
>
用法
:{$rename:{old_field_name:new_field_name}}
> 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" }
>