-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongoose查询汇总.txt
237 lines (148 loc) · 4.64 KB
/
mongoose查询汇总.txt
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#查询每个栏目下的商品数量
{
key:{cat_id:1},
cond:{},
reduce:function(curr,result) {
result.cnt += 1;
},
initial:{cnt:0}
}
#查询每个栏目下价格高于50元的商品数量
{
key:{cat_id:1},
cond:{shop_price:{$gt:50}},
reduce:function(curr,result) {
result.cnt += 1;
},
initial:{cnt:0}
}
#每个栏目下的商品库存量 sum()操作
{
key:{cat_id:1},
cond:{},
reduce:function(curr,result) {
result.num += curr.goods_number;
},
initial:{num:0}
}
#查询每个栏目最贵的商品价格, max()操作
{
key:{cat_id:1},
cond:{},
reduce:function(curr , result) {
if(curr.shop_price > result.max) {
result.max = curr.shop_price;
}
},
initial:{max:0}
}
#同上,查询每个栏目下最便宜的商品价格,同学们自行完成
#查询每个栏目下商品的平均价格
{
key:{cat_id:1},
cond:{},
reduce:function(curr , result) {
result.cnt += 1;
result.sum += curr.shop_price;
},
initial:{sum:0,cnt:0},
finalize:function(result) {
result.avg = result.sum/result.cnt;
}
}
#查询每个栏目下的商品数量
db.collection.aggregate();
[
{$group:{_id:"$cat_id",total:{$sum:1}}}
]
#查询goods下有多少条商品,select count(*) from goods
[
{$group:{_id:null,total:{$sum:1}}}
]
#查询每个栏目下 价格大于50元的商品个数
[
{$match:{shop_price:{$gt:50}}},
{$group:{_id:"$cat_id",total:{$sum:1}}}
]
#查询每个栏目下 价格大于50元的商品个数
#并筛选出"满足条件的商品个数" 大于等于3的栏目
[
{$match:{shop_price:{$gt:50}}},
{$group:{_id:"$cat_id",total:{$sum:1}}},
{$match:{total:{$gte:3}}}
]
#查询每个栏目下的库存量
[
{$group:{_id:"$cat_id" , total:{$sum:"$goods_number"}}},
]
#查询每个栏目下的库存量,并按库存量排序
[
{$group:{_id:"$cat_id" , total:{$sum:"$goods_number"}}},
{$sort:{total:1}}
]
#查询每个栏目下的库存量,并按库存量排序
[
{$group:{_id:"$cat_id" , total:{$sum:"$goods_number"}}},
{$sort:{total:1}},
{$limit:3}
]
#查询每个栏目的商品平均价格,并按平均价格由高到低排序
[
{$group:{_id:"$cat_id" , avg:{$avg:"$shop_price"}}},
{$sort:{avg:-1}}
]
mapReduce 随着"大数据"概念而流行.
其实mapReduce的概念非常简单,
从功能上说,相当于RDBMS的 group 操作
mapReduce的真正强项在哪?
答:在于分布式,当数据非常大时,像google,有N多数据中心,
数据都不在地球的一端,用group力所不及.
group既然不支持分布式,单台服务器的运算能力必然是有限的.
而mapRecuce支持分布式,支持大量的服务器同时工作,
用蛮力来统计.
mapRecuce的工作过程:
map-->映射
reduce->归约
map: 先是把属于同一个组的数据,映射到一个数组上.cat_id-3 [23,2,6,7]
reduce: 把数组(同一组)的数据,进行运算.
用mapReduce计算每个栏目的库存总量
map函数
var map = function() {
emit(this.cat_id,this.goods_number);
}
var reduce = function(cat_id,numbers) {
return Array.sum(numbers);
}
db.goods.mapReduce(map,reduce,{out:'res'});
#用mapReduce计算每个栏目下商品的平均价格
var map = function() {
emit(this.cat_id,this.shop_price);
}
var reduce = function(cat_id,values) {
return Array.avg(values);
}
db.goods.mapReduce(map,reduce,{out:'res'});
查出满足以下条件的商品
1.1:主键为32的商品
db.goods.find({goods_id:32});
1.2:不属第3栏目的所有商品($ne)
db.goods.find({cat_id:{$ne:3}},{goods_id:1,cat_id:1,goods_name:1});
1.3:本店价格高于3000元的商品{$gt}
db.goods.find({shop_price:{$gt:3000}},{goods_name:1,shop_price:1});
1.4:本店价格低于或等于100元的商品($lte)
db.goods.find({shop_price:{$lte:100}},{goods_name:1,shop_price:1});
1.5:取出第4栏目或第11栏目的商品($in)
db.goods.find({cat_id:{$in:[4,11]}},{goods_name:1,shop_price:1});
1.6:取出100<=价格<=500的商品($and)
db.goods.find({$and:[{price:{$gt:100},{$price:{$lt:500}}}]);
1.7:取出不属于第3栏目且不属于第11栏目的商品($and $nin和$nor分别实现)
db.goods.find({$and:[{cat_id:{$ne:3}},{cat_id:{$ne:11}}]},{goods_name:1,cat_id:1})
db.goods.find({cat_id:{$nin:[3,11]}},{goods_name:1,cat_id:1});
db.goods.find({$nor:[{cat_id:3},{cat_id:11}]},{goods_name:1,cat_id:1});
1.8:取出价格大于100且小于300,或者大于4000且小于5000的商品()
db.goods.find({$or:[{$and:[{shop_price:{$gt:100}},{shop_price:{$lt:300}}]},{$and:[{shop_price:{$gt:4000}},{shop_price:{$lt:5000}}]}]},{goods_name:1,shop_price:1});
1.9:取出goods_id%5 == 1, 即,1,6,11,..这样的商品
db.goods.find({goods_id:{$mod:[5,1]}});
1.10:取出有age属性的文档
db.stu.find({age:{$exists:1}});
含有age属性的文档将会被查出