mongodb更新操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
除了查询条件,还可以使用修改器对文档进行更新。
 
1. $inc
 
> db.tianyc03.find()
{ "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "name" : "xtt", "age" : 11 }
> db.tianyc03.update({name:'xtt',age:11},{'$inc':{age:5}})
> db.tianyc03.find()
{ "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "name" : "xtt", "age" : 16 }
 
#$inc还可以添加列。
 
> db.tianyc04.find()
{ "_id" : ObjectId("510c83ad13d7e50c2281b048"), "wc" : 1 }
> db.tianyc04.update({wc:1},{$inc:{score:50}})
> db.tianyc04.find()
{ "_id" : ObjectId("510c83ad13d7e50c2281b048"), "score" : 50, "wc" : 1 }
>
 
2. $set
 
用来指定一个键的值,若该键不存在,则创建。如果需要删除该键,使用$unset。
 
#新增加列sex
> db.tianyc03.update({name:'xtt',age:16},{$set:{sex:'m'}})
> db.tianyc03.find()
{ "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "age" : 16, "name" : "xtt", "sex" : "m" }
#修改列sex
> db.tianyc03.update({name:'xtt',age:16},{$set:{sex:'male'}})
> db.tianyc03.find()
{ "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "age" : 16, "name" : "xtt", "sex" : "male" }
#改变列sex数据类型
> db.tianyc03.update({name:'xtt',age:16},{$set:{sex:1}})
> db.tianyc03.find()
{ "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "age" : 16, "name" : "xtt", "sex" : 1 }
#删除列sex
> db.tianyc03.update({name:'xtt',age:16},{$unset:{sex:1}})
> db.tianyc03.find()
{ "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "age" : 16, "name" : "xtt" }
 
 3. $push
 
用来增加数组。若没有改列,则会自动增加。
 
> db.tianyc03.find()
{ "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "age" : 16, "name" : "xtt" }
 
> db.tianyc03.update({'name':'xtt'},{$push:{companies:'neu'}})
> db.tianyc03.find()
{ "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "age" : 16, "companies" : [ "neu" ], "name" : "xtt" }
 
> db.tianyc03.update({'name':'xtt'},{$push:{companies:'alibaba'}})
> db.tianyc03.find()
{ "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "age" : 16, "companies" : [ "neu", "alibaba" ], "name" : "xtt" }
 
4. $addToSet
 
#push操作不会检查数组中元素是否重复,如果需要将不重复的数据加入数组,需要使用$addToSet
 
> db.tianyc03.update({'name':'xtt'},{$addToSet:{companies:'alibaba'}})
 
> db.tianyc03.find()
{ "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "age" : 16, "companies" : [ "neu", "alibaba", "alibaba" ], "name" : "xtt" }
> db.tianyc03.update({'name':'xtt'},{$addToSet:{companies:'congxing'}})
> db.tianyc03.find()
{ "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "age" : 16, "companies" : [ "neu", "alibaba", "alibaba", "congxing" ], "name" : "xtt" }
> db.tianyc03.update({'name':'xtt'},{$addToSet:{companies:'congxing'}})
> db.tianyc03.find()
{ "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "age" : 16, "companies" : [ "neu", "alibaba", "alibaba", "congxing" ], "name" : "xtt" }
>
 
5. $pop
 
通过$pop可以从数组中移除数据。此时数据被看成是一个队列。使用{$pop:{key:1}}将从队列末尾移除元素,使用{$pop:{key:-1}}将从队列开头移除元素。
 
> db.tianyc03.update({'name':'xtt'},{$addToSet:{companies:['c1','c2']}})
> db.tianyc03.find()
{ "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "age" : 16, "companies" : [ "neu", "alibaba", "alibaba", "congxing", [ "c1", "c2" ] ], "name" : "xtt" }
 
> db.tianyc03.update({'name':'xtt'},{$pop:{companies:1}})
> db.tianyc03.find()
{ "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "age" : 16, "companies" : [ "neu", "alibaba", "alibaba", "congxing" ], "name" : "xtt" }
>
> db.tianyc03.update({'name':'xtt'},{$pop:{companies:-1}})
> db.tianyc03.find()
{ "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "age" : 16, "companies" : [ "alibaba", "alibaba", "congxing" ], "name" : "xtt" }
>
 
6. $pull
 
除了 $pop,也可以使用 $pull 来删除数组中的元素,此时可以根据元素值进行匹配,将匹配上的元素全部删除。
 
> db.tianyc03.update({'name':'xtt'},{$addToSet:{companies:'teletek'}})
> db.tianyc03.find()
{ "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "age" : 16, "companies" : [ "alibaba", "alibaba", "congxing", "teletek" ], "name" : "xtt" }
> db.tianyc03.update({},{$pull:{companies:'congxing'}})
> db.tianyc03.find()
{ "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "age" : 16, "companies" : [ "alibaba", "alibaba", "teletek" ], "name" : "xtt" }
 
> db.tianyc03.update({},{$pull:{companies:'alibaba'}})
> db.tianyc03.find()
{ "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "age" : 16, "companies" : [ "teletek" ], "name" : "xtt" }
>

  转自:http://www.cnblogs.com/yuechaotian/archive/2013/02/04/2890266.html

posted @   mofy  阅读(186)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示