@@ -17,19 +17,19 @@ sudo apt-get install -y mongodb-org
17
17
{% endhighlight %}
18
18
19
19
Try MongoDB: http://try.mongodb.org
20
- ···
20
+ ```
21
21
停止服务:
22
22
sudo service mongod stop
23
23
启动服务:
24
24
sudo service mongod start
25
25
切换数据库
26
26
use 数据库名
27
- ···
27
+ ```
28
28
29
29
2 . 插入命令
30
30
31
31
插入数据到students collection
32
- {% highlight javascript %}
32
+ ```
33
33
db.students.insert({name: "张三", school: {name: "清华大学", city: "北京"}, age: 19, gpa: 3.97})
34
34
35
35
db.students.insert({name: "李四", school: {name: "北京大学", city: "北京"}, age: 20, gpa: 3.3})
@@ -41,10 +41,11 @@ db.students.insert({name: "小牛", school: {name: "哈工大", city: "哈尔滨
41
41
db.students.insert({name: "小马", school: {name: "交通大学", city: "西安"}, age: 21, gpa: 3.70})
42
42
43
43
db.students.insert({name: "小朱"})
44
- {% endhighlight %}
44
+
45
+ ```
45
46
46
47
3 . 查询命令
47
- {% highlight javascript %}
48
+ ```
48
49
值精确匹配的查询
49
50
db.students.find({name: "张三"})
50
51
两个条件(逻辑与)
@@ -69,9 +70,10 @@ db.students.find({school:{$exists:false}})
69
70
db.students.find({name: /^小/})
70
71
db.students.find({name: /.*四/})
71
72
{% endhighlight %}
73
+ ```
72
74
73
75
3 . Update命令
74
-
76
+ 暂无
75
77
76
78
77
79
4 . Bson Document
@@ -80,6 +82,7 @@ http://docs.mongodb.org/manual/data-modeling/
80
82
81
83
82
84
6 . 内嵌数组查询
85
+ ```
83
86
db.students.insert({name: "张三", school: {name: "清华大学", city: "北京"}, courses:[{name:"MongoDB", grade:88, quiz:[9,8,9,10]},{name:"Java", grade:99,quiz:[3,2,1,5]}], age: 19, gpa: 3.97})
84
87
85
88
db.students.insert({name: "李四", school: {name: "北京大学", city: "北京"}, courses:[{name:"MongoDB", grade:86, quiz:[5,4,3,7]},{name:"Java", grade:92}, {name:"C++", grade:65}], age: 20, gpa: 3.3})
@@ -91,9 +94,10 @@ db.students.insert({name: "小牛", school: {name: "哈工大", city: "哈尔滨
91
94
db.students.insert({name: "小马", school: {name: "交通大学", city: "西安"}, courses:[{name:"MongoDB", grade:96, quiz:[5,4,3,7]}], age: 21, gpa: 3.70})
92
95
93
96
db.students.insert({name: "小朱"})
94
-
97
+ ```
95
98
96
99
7 . 地理位置查询
100
+ ```
97
101
db.pois.insert({name:"AAA Store", loc:{type:"Point", coordinates:[70,30]}})
98
102
db.pois.insert({name:"BBB Bank", loc:{type:"Point", coordinates:[69.99,30.01]}})
99
103
db.pois.insert({name:"CCC Park", loc:{type:"Polygon", coordinates:[[[70,30],[71,31],[71,30],[70,30]]]}})
@@ -130,29 +134,31 @@ db.runCommand(
130
134
maxDistance: 7000
131
135
}
132
136
)
133
-
137
+ ```
134
138
9 . 全文搜索
135
-
139
+ ```
136
140
db.text.insert({content:"text performs a text search on the content of the fields indexed with a text index."})
137
141
db.text.insert({content:"When dealing with a small number of documents, it is possible for the full-text-search engine to directly scan the contents of the documents with each query, a strategy called 'serial scanning.' This is what some rudimentary tools, such as grep, do when searching."})
138
142
db.text.insert({content:"Soros enjoys playing mongo."})
139
143
db.text.insert({content:"Why don't you use mongo-db?"})
140
144
141
-
145
+ ```
142
146
143
147
144
148
145
149
10 . 学生成绩Group命令
150
+ ```
146
151
db.students.group({
147
152
key:{age:1},
148
153
cond: {age:{$exists:true}},
149
154
reduce:function(cur, result) { result.count += 1; result.total_gpa += cur.gpa; result.ava_gpa = result.total_gpa / result.count;},
150
155
initial: { count: 0 , total_gpa: 0}
151
156
})
152
-
157
+ ```
153
158
11 . 数据聚合 -- 流水线
154
159
各年龄段平均GPA计算和排序
155
160
161
+ ```
156
162
db.students.aggregate([
157
163
{$match:{age:{$exists:true}}},
158
164
{$group:{_id:"$age", count: {$sum:1}, total_gpa:{$sum:"$gpa"}}},
@@ -181,10 +187,10 @@ db.students.aggregate(
181
187
{$sort: {cc:-1}}
182
188
]
183
189
)
184
-
190
+ ```
185
191
186
192
12 数据聚合MapReduce
187
-
193
+ ```
188
194
按age分组计算平均gpa (错误)
189
195
db.students.mapReduce(
190
196
function(){
@@ -271,4 +277,4 @@ db.students.aggregate([
271
277
272
278
])
273
279
274
-
280
+ ```
0 commit comments