@@ -17,19 +17,19 @@ sudo apt-get install -y mongodb-org
1717{% endhighlight %}
1818
1919Try MongoDB: http://try.mongodb.org
20- ···
20+ ```
2121停止服务:
2222 sudo service mongod stop
2323启动服务:
2424 sudo service mongod start
2525切换数据库
2626 use 数据库名
27- ···
27+ ```
2828
29292 . 插入命令
3030
3131插入数据到students collection
32- {% highlight javascript %}
32+ ```
3333db.students.insert({name: "张三", school: {name: "清华大学", city: "北京"}, age: 19, gpa: 3.97})
3434
3535db.students.insert({name: "李四", school: {name: "北京大学", city: "北京"}, age: 20, gpa: 3.3})
@@ -41,10 +41,11 @@ db.students.insert({name: "小牛", school: {name: "哈工大", city: "哈尔滨
4141db.students.insert({name: "小马", school: {name: "交通大学", city: "西安"}, age: 21, gpa: 3.70})
4242
4343db.students.insert({name: "小朱"})
44- {% endhighlight %}
44+
45+ ```
4546
46473 . 查询命令
47- {% highlight javascript %}
48+ ```
4849值精确匹配的查询
4950db.students.find({name: "张三"})
5051两个条件(逻辑与)
@@ -69,9 +70,10 @@ db.students.find({school:{$exists:false}})
6970db.students.find({name: /^小/})
7071db.students.find({name: /.*四/})
7172{% endhighlight %}
73+ ```
7274
73753 . Update命令
74-
76+ 暂无
7577
7678
77794 . Bson Document
@@ -80,6 +82,7 @@ http://docs.mongodb.org/manual/data-modeling/
8082
8183
82846 . 内嵌数组查询
85+ ```
8386db.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})
8487
8588db.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: "哈尔滨
9194db.students.insert({name: "小马", school: {name: "交通大学", city: "西安"}, courses:[{name:"MongoDB", grade:96, quiz:[5,4,3,7]}], age: 21, gpa: 3.70})
9295
9396db.students.insert({name: "小朱"})
94-
97+ ```
9598
96997 . 地理位置查询
100+ ```
97101db.pois.insert({name:"AAA Store", loc:{type:"Point", coordinates:[70,30]}})
98102db.pois.insert({name:"BBB Bank", loc:{type:"Point", coordinates:[69.99,30.01]}})
99103db.pois.insert({name:"CCC Park", loc:{type:"Polygon", coordinates:[[[70,30],[71,31],[71,30],[70,30]]]}})
@@ -130,29 +134,31 @@ db.runCommand(
130134 maxDistance: 7000
131135 }
132136)
133-
137+ ```
1341389 . 全文搜索
135-
139+ ```
136140db.text.insert({content:"text performs a text search on the content of the fields indexed with a text index."})
137141db.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."})
138142db.text.insert({content:"Soros enjoys playing mongo."})
139143db.text.insert({content:"Why don't you use mongo-db?"})
140144
141-
145+ ```
142146
143147
144148
14514910 . 学生成绩Group命令
150+ ```
146151db.students.group({
147152 key:{age:1},
148153 cond: {age:{$exists:true}},
149154 reduce:function(cur, result) { result.count += 1; result.total_gpa += cur.gpa; result.ava_gpa = result.total_gpa / result.count;},
150155 initial: { count: 0 , total_gpa: 0}
151156 })
152-
157+ ```
15315811 . 数据聚合 -- 流水线
154159各年龄段平均GPA计算和排序
155160
161+ ```
156162db.students.aggregate([
157163 {$match:{age:{$exists:true}}},
158164 {$group:{_id:"$age", count: {$sum:1}, total_gpa:{$sum:"$gpa"}}},
@@ -181,10 +187,10 @@ db.students.aggregate(
181187 {$sort: {cc:-1}}
182188 ]
183189)
184-
190+ ```
185191
18619212 数据聚合MapReduce
187-
193+ ```
188194按age分组计算平均gpa (错误)
189195db.students.mapReduce(
190196 function(){
@@ -271,4 +277,4 @@ db.students.aggregate([
271277
272278])
273279
274-
280+ ```
0 commit comments