@@ -46,7 +46,7 @@ db.students.insert({name: "小朱"})
4646{% endhighlight %}
4747
48483 . 查询命令
49- ```
49+ {% highlight javascript %}
5050值精确匹配的查询
5151db.students.find({name: "张三"})
5252两个条件(逻辑与)
@@ -71,7 +71,7 @@ db.students.find({school:{$exists:false}})
7171db.students.find({name: /^小/})
7272db.students.find({name: /.* 四/})
7373
74- ```
74+ {% endhighlight %}
7575
76763 . Update命令
7777暂无
@@ -83,7 +83,7 @@ http://docs.mongodb.org/manual/data-modeling/
8383
8484
85856 . 内嵌数组查询
86- ```
86+ {% highlight javascript %}
8787db.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})
8888
8989db.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})
@@ -95,10 +95,10 @@ db.students.insert({name: "小牛", school: {name: "哈工大", city: "哈尔滨
9595db.students.insert({name: "小马", school: {name: "交通大学", city: "西安"}, courses:[ {name:"MongoDB", grade:96, quiz:[ 5,4,3,7] }] , age: 21, gpa: 3.70})
9696
9797db.students.insert({name: "小朱"})
98- ```
98+ {% endhighlight %}
9999
1001007 . 地理位置查询
101- ```
101+ {% highlight javascript %}
102102db.pois.insert({name:"AAA Store", loc:{type:"Point", coordinates:[ 70,30] }})
103103db.pois.insert({name:"BBB Bank", loc:{type:"Point", coordinates:[ 69.99,30.01] }})
104104db.pois.insert({name:"CCC Park", loc:{type:"Polygon", coordinates:[[[ 70,30] ,[ 71,31] ,[ 71,30] ,[ 70,30]]] }})
@@ -135,31 +135,33 @@ db.runCommand(
135135 maxDistance: 7000
136136 }
137137)
138- ```
138+ {% endhighlight %}
1391399 . 全文搜索
140- ```
140+ {% highlight javascript %}
141141db.text.insert({content:"text performs a text search on the content of the fields indexed with a text index."})
142142db.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."})
143143db.text.insert({content:"Soros enjoys playing mongo."})
144144db.text.insert({content:"Why don't you use mongo-db?"})
145145
146- ```
146+ {% endhighlight %}
147147
148148
149149
15015010 . 学生成绩Group命令
151- ```
151+ {% highlight javascript %}
152152db.students.group({
153153 key:{age:1},
154154 cond: {age:{$exists: true }},
155155 reduce: function (cur, result) { result.count += 1; result.total_gpa += cur.gpa; result.ava_gpa = result.total_gpa / result.count;},
156156 initial: { count: 0 , total_gpa: 0}
157157 })
158- ```
158+ {% endhighlight %}
15915911 . 数据聚合 -- 流水线
160+
161+ {% highlight javascript %}
160162各年龄段平均GPA计算和排序
161163
162- ```
164+
163165db.students.aggregate([
164166 {$match:{age:{$exists: true }}},
165167 {$group:{_ id:"$age", count: {$sum:1}, total_gpa:{$sum:"$gpa"}}},
@@ -188,10 +190,10 @@ db.students.aggregate(
188190 {$sort: {cc:-1}}
189191 ]
190192)
191- ```
193+ {% endhighlight %}
192194
19319512 数据聚合MapReduce
194- ```
196+ {% highlight javascript %}
195197按age分组计算平均gpa (错误)
196198db.students.mapReduce(
197199 function(){
@@ -278,4 +280,4 @@ db.students.aggregate([
278280
279281] )
280282
281- ```
283+ {% endhighlight %}
0 commit comments