Skip to content

Commit 5a3dc17

Browse files
committed
mongodb notes refine
1 parent 819dd49 commit 5a3dc17

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

_posts/2015-1-1-MongoDB-Notes.md

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,19 @@ sudo apt-get install -y mongodb-org
1717
{% endhighlight %}
1818

1919
Try 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

2929
2. 插入命令
3030

3131
插入数据到students collection
32-
{% highlight javascript %}
32+
```
3333
db.students.insert({name: "张三", school: {name: "清华大学", city: "北京"}, age: 19, gpa: 3.97})
3434
3535
db.students.insert({name: "李四", school: {name: "北京大学", city: "北京"}, age: 20, gpa: 3.3})
@@ -41,10 +41,11 @@ db.students.insert({name: "小牛", school: {name: "哈工大", city: "哈尔滨
4141
db.students.insert({name: "小马", school: {name: "交通大学", city: "西安"}, age: 21, gpa: 3.70})
4242
4343
db.students.insert({name: "小朱"})
44-
{% endhighlight %}
44+
45+
```
4546

4647
3. 查询命令
47-
{% highlight javascript %}
48+
```
4849
值精确匹配的查询
4950
db.students.find({name: "张三"})
5051
两个条件(逻辑与)
@@ -69,9 +70,10 @@ db.students.find({school:{$exists:false}})
6970
db.students.find({name: /^小/})
7071
db.students.find({name: /.*四/})
7172
{% endhighlight %}
73+
```
7274

7375
3. Update命令
74-
76+
暂无
7577

7678

7779
4. Bson Document
@@ -80,6 +82,7 @@ http://docs.mongodb.org/manual/data-modeling/
8082

8183

8284
6. 内嵌数组查询
85+
```
8386
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})
8487
8588
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: "哈尔滨
9194
db.students.insert({name: "小马", school: {name: "交通大学", city: "西安"}, courses:[{name:"MongoDB", grade:96, quiz:[5,4,3,7]}], age: 21, gpa: 3.70})
9295
9396
db.students.insert({name: "小朱"})
94-
97+
```
9598

9699
7. 地理位置查询
100+
```
97101
db.pois.insert({name:"AAA Store", loc:{type:"Point", coordinates:[70,30]}})
98102
db.pois.insert({name:"BBB Bank", loc:{type:"Point", coordinates:[69.99,30.01]}})
99103
db.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+
```
134138
9. 全文搜索
135-
139+
```
136140
db.text.insert({content:"text performs a text search on the content of the fields indexed with a text index."})
137141
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."})
138142
db.text.insert({content:"Soros enjoys playing mongo."})
139143
db.text.insert({content:"Why don't you use mongo-db?"})
140144
141-
145+
```
142146

143147

144148

145149
10. 学生成绩Group命令
150+
```
146151
db.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+
```
153158
11. 数据聚合 -- 流水线
154159
各年龄段平均GPA计算和排序
155160

161+
```
156162
db.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

186192
12 数据聚合MapReduce
187-
193+
```
188194
按age分组计算平均gpa (错误)
189195
db.students.mapReduce(
190196
function(){
@@ -271,4 +277,4 @@ db.students.aggregate([
271277
272278
])
273279
274-
280+
```

0 commit comments

Comments
 (0)