Skip to content

Commit f02355c

Browse files
committed
update docs
1 parent 4b61d23 commit f02355c

File tree

5 files changed

+170
-30
lines changed

5 files changed

+170
-30
lines changed

docs/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ footer: CC-BY-SA-4.0 Licensed | Copyright © 2018-Now Dunwu
8888
- [HBase Java API](https://github.com/dunwu/bigdata-tutorial/blob/master/docs/hbase/hbase-api.md)
8989
- [HBase 配置](https://github.com/dunwu/bigdata-tutorial/blob/master/docs/hbase/hbase-ops.md)
9090

91+
#### MongoDB
92+
93+
- [MongoDB 应用指南](nosql/mongodb/mongodb-quickstart.md)
94+
- [MongoDB 运维](nosql/mongodb/mongodb-ops.md)
95+
9196
### 中间件
9297

9398
- [版本管理中间件 flyway](middleware/flyway.md)

docs/nosql/mongodb/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# MongoDB 教程
2+
3+
## 📖 内容
4+
5+
### [MongoDB 应用指南](mongodb-quickstart.md)
6+
7+
### [MongoDB 建模](mongodb-model.md)
8+
9+
### [MongoDB 运维](mongodb-ops.md)
10+
11+
## 📚 资料
12+
13+
- **官方**
14+
- [MongoDB 官网](https://www.mongodb.com/)
15+
- [MongoDBGithub](https://github.com/mongodb/mongo)
16+
- [MongoDB 官方免费教程](https://university.mongodb.com/)
17+
- **教程**
18+
- [MongoDB 教程](https://www.runoob.com/mongodb/mongodb-tutorial.html)
19+
- [MongoDB 高手课](https://time.geekbang.org/course/intro/100040001)
20+
21+
## 🚪 传送
22+
23+
◾ 🏠 [DB-TUTORIAL 首页](https://github.com/dunwu/db-tutorial) ◾ 🎯 [我的博客](https://github.com/dunwu/blog)

docs/nosql/mongodb/mongodb-model.md

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# MongoDB 建模
2+
3+
MongoDB 的数据模式是一种灵活模式,关系型数据库要求你在插入数据之前必须先定义好一个表的模式结构,而 MongoDB 的集合则并不限制 document 结构。这种灵活性让对象和数据库文档之间的映射变得很容易。即使数据记录之间有很大的变化,每个文档也可以很好的映射到各条不同的记录。 当然在实际使用中,同一个集合中的文档往往都有一个比较类似的结构。
4+
5+
数据模型设计中最具挑战性的是在应用程序需求,数据库引擎性能要求和数据读写模式之间做权衡考量。当设计数据模型的时候,一定要考虑应用程序对数据的使用模式(如查询,更新和处理)以及数据本身的天然结构。
6+
7+
## MongoDB 数据建模入门
8+
9+
> 参考:https://docs.mongodb.com/guides/server/introduction/#what-you-ll-need
10+
11+
### (一)定义数据集
12+
13+
当需要建立数据存储时,您的首要任务是思考以下问题:我想存储哪些数据?这些字段之间如何关联?
14+
15+
假设这样一个场景:我们需要建立数据库以跟踪物料及其数量,大小,标签和等级。
16+
17+
如果是存储在 RDBMS,可能以下的数据表:
18+
19+
| name | quantity | size | status | tags | rating |
20+
| :------- | :------- | :---------- | :----- | :----------------------- | :----- |
21+
| journal | 25 | 14x21,cm | A | brown, lined | 9 |
22+
| notebook | 50 | 8.5x11,in | A | college-ruled,perforated | 8 |
23+
| paper | 100 | 8.5x11,in | D | watercolor | 10 |
24+
| planner | 75 | 22.85x30,cm | D | 2019 | 10 |
25+
| postcard | 45 | 10x,cm | D | double-sided,white | 2 |
26+
27+
### (二)思考 JSON 结构
28+
29+
从上例中可以看出,表似乎是存储数据的好地方,但该数据集中的字段需要多个值,如果在单个列中建模,则不容易搜索或显示(对于 例如–大小和标签)。
30+
31+
在SQL数据库中,您可以通过创建关系表来解决此问题。
32+
33+
在MongoDB中,数据存储为文档。 这些文档以JSON(JavaScript对象表示法)格式存储在MongoDB中。 JSON文档支持嵌入式字段,因此相关数据和数据列表可以与文档一起存储,而不是与外部表一起存储。
34+
35+
JSON格式为键/值对。 在JSON文档中,字段名和值用冒号分隔,字段名和值对用逗号分隔,并且字段集封装在“大括号”(`{}`)中。
36+
37+
如果要开始对上面的行之一进行建模,例如此行:
38+
39+
| name | quantity | size | status | tags | rating |
40+
| :------- | :------- | :-------- | :----- | :----------------------- | :----- |
41+
| notebook | 50 | 8.5x11,in | A | college-ruled,perforated | 8 |
42+
43+
您可以从name和quantity字段开始。 在JSON中,这些字段如下所示:
44+
45+
```json
46+
{"name": "notebook", "qty": 50}
47+
```
48+
49+
### (三)确定哪些字段作为嵌入式数据
50+
51+
接下来,需要确定哪些字段可能需要多个值。可以考虑将这些字段作为嵌入式文档或嵌入式文档中的 列表/数组 对象。
52+
53+
例如,在上面的示例中,size 可能包含三个字段:
54+
55+
```json
56+
{ "h": 11, "w": 8.5, "uom": "in" }
57+
```
58+
59+
And some items have multiple ratings, so `ratings` might be represented as a list of documents containing the field `scores`:
60+
61+
```json
62+
[ { "score": 8 }, { "score": 9 } ]
63+
```
64+
65+
And you might need to handle multiple tags per item. So you might store them in a list too.
66+
67+
```json
68+
[ "college-ruled", "perforated" ]
69+
```
70+
71+
Finally, a JSON document that stores an inventory item might look like this:
72+
73+
```json
74+
{
75+
"name": "notebook",
76+
"qty": 50,
77+
"rating": [ { "score": 8 }, { "score": 9 } ],
78+
"size": { "height": 11, "width": 8.5, "unit": "in" },
79+
"status": "A",
80+
"tags": [ "college-ruled", "perforated"]
81+
}
82+
```
83+
84+
This looks very different from the tabular data structure you started with in Step 1.
85+
86+
## 文档结构
87+
88+
设计基于 MongoDB 的应用程序的数据模型时的关键就是选择合适的文档结构以及确定应用程序如何描述数据之间的关系。有两种方式可以用来描述这些关系:引用及内嵌。
89+
90+
### References - 引用
91+
92+
引用方式通过存储链接或者引用信息来实现两个不同文档之间的关联。应用程序可以通过解析这些数据库引用来访问相关数据。简单来讲,这就是规范化的数据模型。
93+
94+
![Data model using references to link documents. Both the ``contact`` document and the ``access`` document contain a reference to the ``user`` document.](https://mongoing.com/docs/_images/data-model-normalized.png)
95+
96+
### Embedded Data - 内嵌
97+
98+
内嵌方式指的是把相关联的数据保存在同一个文档结构之内。MongoDB的文档结构允许一个字段或者一个数组内的值为一个嵌套的文档。这种冗余的数据模型可以让应用程序在一个数据库操作内完成对相关数据的读取或修改。
99+
100+
![Data model with embedded fields that contain all related information.](https://mongoing.com/docs/_images/data-model-denormalized.png)
101+
102+
## 参考资料
103+
104+
- **官方**
105+
- [MongoDB 官网](https://www.mongodb.com/)
106+
- [MongoDBGithub](https://github.com/mongodb/mongo)
107+
- [MongoDB 官方免费教程](https://university.mongodb.com/)
108+
- **教程**
109+
- [MongoDB 教程](https://www.runoob.com/mongodb/mongodb-tutorial.html)
110+
- [MongoDB 高手课](https://time.geekbang.org/course/intro/100040001)

docs/nosql/mongodb/mongodb-ops.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
<!-- TOC depthFrom:2 depthTo:3 -->
44

55
- [一、MongoDB 安装](#一mongodb-安装)
6-
- [Windows](#windows)
7-
- [Linux](#linux)
8-
- [设置用户名、密码](#设置用户名密码)
6+
- [Windows](#windows)
7+
- [Linux](#linux)
8+
- [设置用户名、密码](#设置用户名密码)
99
- [参考资料](#参考资料)
1010

1111
<!-- /TOC -->
@@ -122,5 +122,5 @@ Successfully added user: {
122122

123123
- [MongoDB 官网](https://www.mongodb.com/)
124124
- [MongoDBGithub](https://github.com/mongodb/mongo)
125-
- [MongoDB 官方免费教程——MongoDB University](https://link.zhihu.com/?target=https%3A//university.mongodb.com/)
126-
- [MongoDB 教程](https://www.runoob.com/mongodb/mongodb-tutorial.html)
125+
- [MongoDB 官方免费教程](https://university.mongodb.com/)
126+
- [MongoDB 教程](https://www.runoob.com/mongodb/mongodb-tutorial.html)

docs/nosql/mongodb/mongodb-quickstart.md

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,22 @@
33
<!-- TOC depthFrom:2 depthTo:3 -->
44

55
- [简介](#简介)
6+
- [MongoDB 发展](#mongodb-发展)
7+
- [MongoDB vs. RDBMS](#mongodb-vs-rdbms)
8+
- [MongoDB 特性](#mongodb-特性)
69
- [MongoDB 概念](#mongodb-概念)
710
- [MongoDB 数据类型](#mongodb-数据类型)
8-
- [MongoDB SQL](#mongodb-sql)
9-
- [数据库操作](#数据库操作)
10-
- [集合操作](#集合操作)
11-
- [文档操作](#文档操作)
12-
- [索引操作](#索引操作)
13-
- [聚合操作](#聚合操作)
11+
- [MongoDB CRUD](#mongodb-crud)
12+
- [数据库操作](#数据库操作)
13+
- [集合操作](#集合操作)
14+
- [插入文档操作](#插入文档操作)
15+
- [查询文档操作](#查询文档操作)
16+
- [更新文档操作](#更新文档操作)
17+
- [删除文档操作](#删除文档操作)
18+
- [索引操作](#索引操作)
19+
- [MongoDB 聚合操作](#mongodb-聚合操作)
20+
- [管道](#管道)
21+
- [聚合步骤](#聚合步骤)
1422
- [参考资料](#参考资料)
1523

1624
<!-- /TOC -->
@@ -369,8 +377,6 @@ db.<集合>.find(<JSON>)
369377
68
370378
```
371379
372-
373-
374380
#### 查询逻辑条件
375381

376382
(1)and 条件
@@ -594,7 +600,7 @@ MongoDB 中聚合(aggregate)主要用于处理数据(诸如统计平均值,求
594600

595601
- 接受一系列文档(原始数据);
596602
- 每个步骤对这些文档进行一系列运算;
597-
- 结果文档输出给下一个步骤;
603+
- 结果文档输出给下一个步骤;
598604

599605
聚合操作的基本格式
600606

@@ -606,21 +612,17 @@ db.<集合>.aggregate(pipeline, {options});
606612

607613
### 聚合步骤
608614

609-
| 步骤 | 作用 | SQL 等价运算符 |
610-
| ------------- | ---- | -------------- |
611-
| `$match` | 过滤 | WHERE |
612-
| `$project` | 投影 | AS |
613-
| `$sort` | 排序 | ORDER BY |
614-
| `$group` | 分组 | GROUP BY |
615-
| `$skip` / `$limit` | 结果限制 | SKIP / LIMIT |
616-
| `$lookup` | 左外连接 | LEFT OUTER JOIN |
617-
| `$unwind` | 展开数组 | N/A |
618-
| `$graphLookup` | 图搜索 | N/A |
619-
| `$facet` / `$bucket` | 分面搜索 | N/A |
620-
621-
622-
623-
615+
| 步骤 | 作用 | SQL 等价运算符 |
616+
| -------------------- | -------- | --------------- |
617+
| `$match` | 过滤 | WHERE |
618+
| `$project` | 投影 | AS |
619+
| `$sort` | 排序 | ORDER BY |
620+
| `$group` | 分组 | GROUP BY |
621+
| `$skip` / `$limit` | 结果限制 | SKIP / LIMIT |
622+
| `$lookup` | 左外连接 | LEFT OUTER JOIN |
623+
| `$unwind` | 展开数组 | N/A |
624+
| `$graphLookup` | 图搜索 | N/A |
625+
| `$facet` / `$bucket` | 分面搜索 | N/A |
624626

625627
【示例】
626628

@@ -645,7 +647,7 @@ db.<集合>.aggregate(pipeline, {options});
645647
| \$first | 根据资源文档的排序获取第一个文档数据。 | db.mycol.aggregate([{$group : {_id : "$by_user", first_url : {$first : "$url"}}}]) |
646648
| \$last | 根据资源文档的排序获取最后一个文档数据 | db.mycol.aggregate([{$group : {_id : "$by_user", last_url : {$last : "$url"}}}]) |
647649

648-
-
650+
-
649651

650652
## 参考资料
651653

0 commit comments

Comments
 (0)