@@ -46,7 +46,7 @@ db.students.insert({name: "小朱"})
46
46
{% endhighlight %}
47
47
48
48
3 . 查询命令
49
- ```
49
+ {% highlight javascript %}
50
50
值精确匹配的查询
51
51
db.students.find({name: "张三"})
52
52
两个条件(逻辑与)
@@ -71,7 +71,7 @@ db.students.find({school:{$exists:false}})
71
71
db.students.find({name: /^小/})
72
72
db.students.find({name: /.* 四/})
73
73
74
- ```
74
+ {% endhighlight %}
75
75
76
76
3 . Update命令
77
77
暂无
@@ -83,7 +83,7 @@ http://docs.mongodb.org/manual/data-modeling/
83
83
84
84
85
85
6 . 内嵌数组查询
86
- ```
86
+ {% highlight javascript %}
87
87
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})
88
88
89
89
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})
@@ -95,10 +95,10 @@ db.students.insert({name: "小牛", school: {name: "哈工大", city: "哈尔滨
95
95
db.students.insert({name: "小马", school: {name: "交通大学", city: "西安"}, courses:[ {name:"MongoDB", grade:96, quiz:[ 5,4,3,7] }] , age: 21, gpa: 3.70})
96
96
97
97
db.students.insert({name: "小朱"})
98
- ```
98
+ {% endhighlight %}
99
99
100
100
7 . 地理位置查询
101
- ```
101
+ {% highlight javascript %}
102
102
db.pois.insert({name:"AAA Store", loc:{type:"Point", coordinates:[ 70,30] }})
103
103
db.pois.insert({name:"BBB Bank", loc:{type:"Point", coordinates:[ 69.99,30.01] }})
104
104
db.pois.insert({name:"CCC Park", loc:{type:"Polygon", coordinates:[[[ 70,30] ,[ 71,31] ,[ 71,30] ,[ 70,30]]] }})
@@ -135,31 +135,33 @@ db.runCommand(
135
135
maxDistance: 7000
136
136
}
137
137
)
138
- ```
138
+ {% endhighlight %}
139
139
9 . 全文搜索
140
- ```
140
+ {% highlight javascript %}
141
141
db.text.insert({content:"text performs a text search on the content of the fields indexed with a text index."})
142
142
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."})
143
143
db.text.insert({content:"Soros enjoys playing mongo."})
144
144
db.text.insert({content:"Why don't you use mongo-db?"})
145
145
146
- ```
146
+ {% endhighlight %}
147
147
148
148
149
149
150
150
10 . 学生成绩Group命令
151
- ```
151
+ {% highlight javascript %}
152
152
db.students.group({
153
153
key:{age:1},
154
154
cond: {age:{$exists: true }},
155
155
reduce: function (cur, result) { result.count += 1; result.total_gpa += cur.gpa; result.ava_gpa = result.total_gpa / result.count;},
156
156
initial: { count: 0 , total_gpa: 0}
157
157
})
158
- ```
158
+ {% endhighlight %}
159
159
11 . 数据聚合 -- 流水线
160
+
161
+ {% highlight javascript %}
160
162
各年龄段平均GPA计算和排序
161
163
162
- ```
164
+
163
165
db.students.aggregate([
164
166
{$match:{age:{$exists: true }}},
165
167
{$group:{_ id:"$age", count: {$sum:1}, total_gpa:{$sum:"$gpa"}}},
@@ -188,10 +190,10 @@ db.students.aggregate(
188
190
{$sort: {cc:-1}}
189
191
]
190
192
)
191
- ```
193
+ {% endhighlight %}
192
194
193
195
12 数据聚合MapReduce
194
- ```
196
+ {% highlight javascript %}
195
197
按age分组计算平均gpa (错误)
196
198
db.students.mapReduce(
197
199
function(){
@@ -278,4 +280,4 @@ db.students.aggregate([
278
280
279
281
] )
280
282
281
- ```
283
+ {% endhighlight %}
0 commit comments