-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from mongodb-developer/add-cn-translation
Added Chinese translation
- Loading branch information
Showing
36 changed files
with
1,718 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
i18n/zh/docusaurus-plugin-content-docs/current/1-mongodb-atlas/_category_.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"label": "先决条件", | ||
"position": 2, | ||
"link": { | ||
"type": "generated-index", | ||
"description": "在本节中,您将创建您的 Atlas 账户并启动您的第一个数据库集群。" | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
i18n/zh/docusaurus-plugin-content-docs/current/1-mongodb-atlas/setup-lab.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
--- | ||
description: 设置您的 MongoDB Atlas 账户,创建 MongoDB 集群,并导入数据 | ||
--- | ||
|
||
# 👐 设置实验 | ||
|
||
要跟上进度,您需要: | ||
- 一个 MongoDB Atlas 账户 | ||
- 一个 MongoDB 集群 | ||
- 测试数据。在本例中,这是用于图书馆管理系统的书籍、作者和评论数据。 | ||
|
||
👐 要获取这两者,请打开 [入门实验](https://mongodb-developer.github.io/intro-lab-cn/docs/intro) 并按照步骤操作(只需 10-15 分钟)以准备好您的数据库。完成后返回这里! |
18 changes: 18 additions & 0 deletions
18
...us-plugin-content-docs/current/20-what-is-aggregation/1-what-is-aggregation.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
--- | ||
description: 了解什么是聚合管道 | ||
--- | ||
|
||
# 📘 什么是聚合管道 | ||
|
||
![](/img/20-what-is-aggregation/aggregation-pipeline.png) | ||
|
||
聚合管道类似于使用管道连接的 Unix 命令。 | ||
我们可以构建模块化、可组合的处理管道。 | ||
|
||
一个聚合管道由一个或多个处理文档的 _阶段(stage)_ 组成: | ||
|
||
每个阶段对输入文档执行一个操作。例如,一个阶段可以过滤文档、分组文档和计算值。 | ||
|
||
一个阶段的输出文档被作为输入,并传递给下一个阶段。 | ||
|
||
聚合管道可以返回文档组的结果。例如,返回总值、平均值、最大值和最小值等。 |
62 changes: 62 additions & 0 deletions
62
...rus-plugin-content-docs/current/20-what-is-aggregation/2-sql-vs-aggregation.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
--- | ||
description: 让我们比较一下 SQL 和聚合管道 | ||
--- | ||
|
||
# 📘 SQL 与聚合 | ||
|
||
## SQL | ||
|
||
一个 SQL 查询语句通常以 `SELECT` 开始,在这里我们列出所需的字段,然后是 `FROM` 子句来指定将返回行的表(或在本例中为子查询)。我们可以使用 `WHERE` 进行筛选,并使用 `GROUP` 对数据进行分组。 | ||
|
||
我们从读取 SQL 需从嵌套中理解整个语句的含义。如果嵌套过多,则不易理解。 | ||
|
||
```sql | ||
SELECT | ||
city, | ||
SUM(annual_spend) Total_Spend, | ||
AVG(annual_spend) Average_Spend, | ||
MAX(annual_spend) Max_Spend, | ||
COUNT(annual_spend) customers | ||
FROM ( | ||
SELECT t1.city, customer.annual_spend | ||
FROM customer | ||
LEFT JOIN ( | ||
SELECT address.address_id, city.city, | ||
address.customer_id, address.location | ||
FROM address LEFT JOIN city | ||
ON address.city_id = city.city_id | ||
) AS t1 | ||
ON | ||
(customer.customer_id = t1.customer_id AND | ||
t1.location = "home") | ||
) AS t2 | ||
GROUP BY city; | ||
``` | ||
|
||
## 等效的 MongoDB 聚合管道 | ||
|
||
在这里我们通过三个阶段,一个返回 `address` 数组中每个元素的文档,然后我们筛选出仅包含 `home` 地址位置的文档,最后我们进行分组。正如我们将动手实践并看到的,MongoDB 聚合管道可以分开测试,并且运行逻辑类似于我们的代码结构本身。 | ||
|
||
```js | ||
db.customers.aggregate([ | ||
{ | ||
$unwind: "$address", | ||
}, | ||
{ | ||
$match: {"address.location": "home"} | ||
}, | ||
{ | ||
$group: { | ||
_id: "$address.city", | ||
totalSpend: {$sum: "$annualSpend"}, | ||
averageSpend: {$avg: "$annualSpend"}, | ||
maximumSpend: {$max: "$annualSpend"}, | ||
customers: {$sum: 1} | ||
} | ||
} | ||
]) | ||
``` | ||
|
||
:::info | ||
另请参阅 [SQL 与聚合映射表](https://www.mongodb.com/docs/manual/reference/sql-aggregation-comparison/) | ||
::: |
64 changes: 64 additions & 0 deletions
64
...-plugin-content-docs/current/20-what-is-aggregation/3-structure-aggregation.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
--- | ||
description: 查看组成聚合管道的部分 | ||
--- | ||
|
||
# 📘 聚合管道的结构 | ||
|
||
通常,我们使用 SQL 这种第四代语言来与关系型数据库进行交互。而在 MongoDB 中,我们通过增量步骤来获取和转换数据。 | ||
|
||
一个聚合管道是由 [__阶段__](https://www.mongodb.com/docs/manual/reference/operator/aggregation-pipeline/) 组成的。 | ||
|
||
我们将一个包含多个阶段的数组传递给 `aggregate` 方法,如下所示: | ||
|
||
``` | ||
db.mycollection.aggregate([ | ||
stage1, | ||
stage2, | ||
stage3 | ||
]) | ||
``` | ||
|
||
## 示例 | ||
|
||
一个执行上述 SQL 语句的聚合管道示例如下: | ||
|
||
``` | ||
db.mycollection.aggregate([ | ||
{ | ||
$sort: | ||
/** | ||
* 提供任意数量的字段/排序对。 | ||
*/ | ||
{ | ||
num_mflix_comments: -1, | ||
}, | ||
}, | ||
{ | ||
$limit: | ||
/** | ||
* 提供要限制的文档数量。 | ||
*/ | ||
1, | ||
}, | ||
{ | ||
$unwind: | ||
/** | ||
* path: 数组字段的路径。 | ||
* includeArrayIndex: 索引名称(可选)。 | ||
* preserveNullAndEmptyArrays: 展开 null 和空值(可选)。 | ||
*/ | ||
{ | ||
path: "$cast", | ||
}, | ||
}, | ||
{ | ||
$project: | ||
/** | ||
* 规范:要包含或排除的字段。 | ||
*/ | ||
{ | ||
cast: 1, | ||
}, | ||
}, | ||
]) | ||
``` |
8 changes: 8 additions & 0 deletions
8
i18n/zh/docusaurus-plugin-content-docs/current/20-what-is-aggregation/_category_.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"label": "聚合管道", | ||
"position": 30, | ||
"link": { | ||
"type": "generated-index", | ||
"description": "学习聚合管道的基础知识及其与 SQL 的对比。" | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
...urus-plugin-content-docs/current/30-simple-queries/0-using-library-database.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import Tabs from '@theme/Tabs'; | ||
import TabItem from '@theme/TabItem'; | ||
|
||
# 👐 使用图书馆数据库 | ||
|
||
## 选择图书馆数据库 | ||
|
||
💻 我们将在此实验中使用 `library` 数据库进行所有实践练习。 | ||
如果您还没有执行前置数据导入操作,请将[图书馆数据](https://mdb.link/import-library-data)导入到您的数据库集群中。 | ||
|
||
<Tabs groupId="aggregations"> | ||
<TabItem value="atlas" label="Atlas UI"> | ||
|
||
在聚合管道构建器中选择正确的数据库。 | ||
|
||
<Screenshot src="/img/30-simple-queries/select-db.png" url="http://cloud.mongodb.com/yourcluster" alt="AtlasUI 显示可用的数据库" /> | ||
|
||
</TabItem> | ||
<TabItem value="mongodb-shell" label="MongoDB Shell"> | ||
|
||
要做到这一点,在 MongoDB shell 中输入: | ||
|
||
``` | ||
use library | ||
``` | ||
|
||
您可以显示所有集合: | ||
|
||
``` | ||
show collections | ||
``` | ||
|
||
--- | ||
|
||
🦸♂️ 💻 如何切换到名为 `orders` 的数据库? | ||
|
||
:::info | ||
额外活动:如果您有额外的时间或在家中进行,请执行此操作,动手实验期间不会涉及。 | ||
::: | ||
|
||
<details> | ||
<summary>答案</summary> | ||
<div> | ||
|
||
``` | ||
use orders | ||
``` | ||
|
||
即使这个数据库尚不存在,MongoDB 也可以切换到它。如果我们创建一个集合、用户等,那么这个数据库将被创建。 | ||
|
||
记住通过以下方式返回到 library 数据库: | ||
|
||
``` | ||
use library | ||
``` | ||
</div> | ||
</details> | ||
|
||
## 🦸♂️ 显示所有数据库 | ||
|
||
:::info | ||
额外活动:如果您有额外的时间或在家中进行,请执行此操作,动手实验期间不会涉及。 | ||
::: | ||
|
||
您还可以使用以下命令列出 MongoDB 实例中的其他数据库: | ||
|
||
``` | ||
show databases | ||
``` | ||
|
||
</TabItem> | ||
|
||
</Tabs> |
74 changes: 74 additions & 0 deletions
74
...ocusaurus-plugin-content-docs/current/30-simple-queries/1-empty-aggregation.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import Tabs from '@theme/Tabs'; | ||
import TabItem from '@theme/TabItem'; | ||
|
||
# 👐 空聚合管道 | ||
|
||
## 一个空的聚合 | ||
|
||
这段代码相当于 `SELECT * FROM AUTHORS`。返回一个包含 `authors` 集合中所有文档的 [游标(cursor)](https://www.mongodb.com/docs/manual/reference/method/js-cursor/): | ||
|
||
<Tabs groupId="aggregations"> | ||
|
||
<TabItem value="atlas" label="Atlas UI"> | ||
|
||
<Screenshot src="/img/30-simple-queries/atlas-aggregation.png" url="http://cloud.mongodb.com/" alt="Atlas UI 显示空的聚合管道" /> | ||
|
||
- 打开 `Aggregation` 选项卡。 | ||
- 选择 `Text`。 | ||
- 输入如下一个空数组,注意编辑器中的空数组表示一个空的聚合管道: | ||
|
||
``` | ||
[] | ||
``` | ||
|
||
</TabItem> | ||
|
||
<TabItem value="mongodb-shell" label="MongoDB Shell"> | ||
|
||
``` | ||
db.authors.aggregate([]) | ||
``` | ||
|
||
我们可以遍历返回的游标并通过输入 `it` 获取更多的文档。 | ||
|
||
👐 返回 `books` 集合中的所有文档并迭代获取下一页的书籍。 | ||
|
||
<details> | ||
<summary>答案</summary> | ||
<div> | ||
|
||
``` | ||
db.books.aggregate([]) | ||
it | ||
``` | ||
</div> | ||
</details> | ||
|
||
## 🦸♂️ 游标方法 | ||
|
||
:::info | ||
额外活动:如果您有额外的时间或在家中进行,请执行此操作,动手实验期间不会涉及。 | ||
::: | ||
|
||
一个[游标](https://www.mongodb.com/docs/manual/reference/method/js-cursor/)有几个有用的方法,例如我们可以使用 `itcount` 检查返回游标的大小 | ||
|
||
``` | ||
cursor.itcount() | ||
``` | ||
|
||
👐 在我们之前的空聚合示例中,检查返回游标的大小,我们应该输入什么? | ||
|
||
<details> | ||
<summary>答案</summary> | ||
<div> | ||
|
||
``` | ||
// 因为 db.books.aggregate([]) 返回一个游标,我们可以直接调用 itcount() | ||
db.books.aggregate([]).itcount() | ||
``` | ||
</div> | ||
</details> | ||
|
||
</TabItem> | ||
|
||
</Tabs> |
Oops, something went wrong.