-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
366 additions
and
15 deletions.
There are no files selected for viewing
Binary file added
BIN
+7.5 KB
docs/zh_CN/_images/database/mongodb/a192a6c8d623b2c4fd884d6db0969f89.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+16.2 KB
docs/zh_CN/_images/database/mongodb/a609aaf1de097cc8fa48f04a5531b13d.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+13.3 KB
docs/zh_CN/_images/database/mongodb/ba25d3187412c5fb37ec79bf93d90ad5.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+17.1 KB
docs/zh_CN/_images/database/mongodb/cfb560de9e8b54d9e359a522b3d02961.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+12.9 KB
docs/zh_CN/_images/database/mongodb/fd5d6f3bafc18bef1faa5ff241c7b954.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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,20 @@ | ||
.. | ||
Author: huangxiaoyan | ||
Created time: 2019-11-13 17:23:38 | ||
Last Modified by: huangxiaoyan | ||
Last Modified time: 2019-11-16 15:47:45 | ||
======= | ||
MongoDB | ||
======= | ||
|
||
`MongoDB`_ 是一个基于分布式文件存储的数据库,由 *C++* 语言编写。 | ||
旨在为 *web* 应用提供可扩展的高性能数据存储解决方案。 | ||
|
||
.. toctree:: | ||
:titlesonly: | ||
|
||
环境准备 <prepare.rst> | ||
快速上手 <quick-start.rst> | ||
|
||
.. _MongoDB: https://www.mongodb.com/ |
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,127 @@ | ||
.. | ||
Author: huangxiaoyan | ||
Created time: 2019-11-15 13:24:17 | ||
Last Modified by: huangxiaoyan | ||
Last Modified time: 2019-11-23 10:06:13 | ||
=============== | ||
MongoDB环境准备 | ||
=============== | ||
|
||
在开始学习 *MongoDB* 前,我们需要部署 *MongoDB* 数据库服务。 | ||
但是,鉴于我们学习的重点是使用 *Node.js* 操作 *MongoDB* 数据库, | ||
所以在此我们选择了另外一种方式,来跳过琐碎的部署与配置。 | ||
|
||
感谢 `Docker`_,提供了 *MongoDB* 镜像,供我们快速地安装与使用。 | ||
|
||
安装Docker | ||
=========== | ||
|
||
由于 *Docker* 的用法不在我们的讨论范围。 | ||
因此,本节中只简单介绍如何安装 *Docker* 和在 *Docker* 中运行 *MongoDB* 数据库服务。 | ||
|
||
首先在 `Docker`_ 官网下载 `客户端`_。以下的安装与使用均以 *Mac* 为例。 | ||
下载完毕后,双击 *docker.dmg*,出现如下界面,按提示步骤完成,即安装成功。 | ||
|
||
.. figure:: /_images/database/mongodb/a609aaf1de097cc8fa48f04a5531b13d.png | ||
:width: 400px | ||
|
||
下载MongoDB镜像 | ||
================= | ||
|
||
安装完 *docker* 后,我们接下来需要下载 MongoDB 的镜像。可以在 `官网`_ 上查找有关 *MongoDB* 的镜像包。 | ||
在这,我们拉取 *MongoDB* 的 *3.6* 版本,执行命令: | ||
|
||
.. code-block:: shell-session | ||
docker pull mongo:3.6 | ||
运行MongoDB服务 | ||
=============== | ||
|
||
在运行服务之前,我们需要新建一个 *service.yml* 文件,并写入以下内容: | ||
|
||
.. code-block:: text | ||
// serivce.yml | ||
version: '3.1' | ||
services: | ||
mongo: | ||
image: mongo:3.6 | ||
ports: | ||
- 27017:27017 | ||
environment: | ||
MONGO_INITDB_ROOT_USERNAME: root | ||
MONGO_INITDB_ROOT_PASSWORD: to0r | ||
这个文件定义了一个 *MongoDB* 服务,包含了 *mongdb* 服务的一些信息,包括 *mongodb* 的版本、服务运行的端口、 | ||
数据库用户名和密码。将 *service.yml* 保存在固定目录下,接下来我们需要使用它来创建并运行 *MongoDB* 服务。 | ||
需要注意的是,请尽量不要移动 *service.yml* 文件位置,不然已创建的服务需要重建。 | ||
|
||
打开终端,定位到 *service.yml* 文件所在的目录,并执行如下命令: | ||
|
||
.. code-block:: shell-session | ||
$ docker-compose -f service.yml up -d mongo | ||
执行成功后,我们来查看下 *MongoDB* 服务是否真的已经运行了: | ||
|
||
.. code-block:: shell-session | ||
$ docker ps | ||
可以看到出现了容器的信息: | ||
|
||
.. code-block:: text | ||
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES | ||
c344ce31b67e mongo:3.6 "docker-entrypoint.s…" 8 seconds ago Up 6 seconds 0.0.0.0:27017->27017/tcp docker-service_mongo_1 | ||
此时,*MongoDB* 服务正运行在 *27017* 的端口上。 | ||
|
||
安装Robo3t | ||
========== | ||
|
||
为了方便我们观察数据的变化,我们可以安装一个 *MongoDB* 的 *GUI* 客户端 —— `Robo3t`_ 。 | ||
下载后,双击安装程序,进入安装界面,按照提示进行,此处不再赘述。 | ||
|
||
打开 *Robo3t*,弹出下面弹框,我们可以遵循步骤,来连接 *MongoDB* 数据库。 | ||
|
||
.. figure:: /_images/database/mongodb/a192a6c8d623b2c4fd884d6db0969f89.png | ||
:width: 600px | ||
|
||
点击左上角的 *Create* 按钮,填写对应信息,创建连接。 | ||
|
||
.. figure:: /_images/database/mongodb/fd5d6f3bafc18bef1faa5ff241c7b954.png | ||
:width: 600px | ||
|
||
上图中,我们看到,需要连接的地址为:*localhost:27017*,端口与 *service.yml* 的一样, | ||
不需修改。接下来,我们选择使用 *认证* 的方式连接。 | ||
|
||
.. figure:: /_images/database/mongodb/ba25d3187412c5fb37ec79bf93d90ad5.png | ||
:width: 600px | ||
|
||
从 *service.yml* 文件中,我们可以很轻松得到用户名和密码,并填写。点击 *save* 按钮, | ||
回到主界面后,点击 *connect* 按钮。就这样,我们连接了数据库,并且可以界面化操作数据库。 | ||
|
||
.. figure:: /_images/database/mongodb/cfb560de9e8b54d9e359a522b3d02961.png | ||
:width: 600px | ||
|
||
至此,全部准备工作完毕,我们可以愉快地继续 *MongoDB* 的下一步学习: | ||
|
||
:doc:`quick-start` | ||
|
||
下一步 | ||
====== | ||
|
||
.. include:: /_fragments/next-step-to-wechat-mp.rst | ||
|
||
.. include:: /_fragments/disqus.rst | ||
|
||
.. _Docker: https://www.docker.com/ | ||
.. _客户端: https://hub.docker.com/?overlay=onboarding | ||
.. _官网: https://hub.docker.com/_/mongo | ||
.. _Robo3t: http://nodejs.local:8000/database/mongodb/prepare.html# |
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,190 @@ | ||
.. | ||
Author: huangxiaoyan | ||
Created time: 2019-11-13 17:23:38 | ||
Last Modified by: huangxiaoyan | ||
Last Modified time: 2019-11-17 13:17:27 | ||
=============== | ||
MongoDB快速上手 | ||
=============== | ||
|
||
前面我们介绍了 `NeDB` 数据库,它是一个 *NoSQL* 的嵌入式数据库。今天我们再来介绍一款 *NoSQL* 的数据库 `MongoDB`_ 。 | ||
|
||
在开始学习前,需要准备 *MongoDB* 的环境。如果还没有 *MongoDB* 环境,请先阅读 :doc:`prepare` 一节。 | ||
|
||
安装mongodb | ||
=========== | ||
|
||
*npm* 库提供了 `mongodb模块`_ 。执行如下命令,安装该模块: | ||
|
||
.. code-block:: shell-session | ||
$ npm install mongodb --save | ||
引入依赖 | ||
======== | ||
|
||
使用 *require* 引入 *mongodb* 模块,并获取 *MongoClient* 对象。 | ||
|
||
.. literalinclude:: /_src/database/mongodb/demo/demo.js | ||
:language: javascript | ||
:lines: 1 | ||
:linenos: | ||
|
||
连接数据库服务器 | ||
================ | ||
|
||
与 *NeDB* 不同的是,我们在操作 *MongoDB* 数据库前,需要先连接数据库。 | ||
|
||
.. code-block:: javascript | ||
:linenos: | ||
const url = 'mongodb://root:to0r@localhost:27017' | ||
MongoClient.connect( | ||
url, | ||
function(err, client) { | ||
// 其他数据操作 | ||
... | ||
} | ||
) | ||
使用 *connect* 方法建立连接,*connect* 方法接收两个参数: *url* 和 *callback*。 | ||
其中,*url* 表示数据库服务器的地址,格式为: | ||
|
||
.. code-block:: text | ||
mongodb://{username}:{password}@{host}:{port} | ||
回到本文中具体的例子,即: | ||
|
||
.. literalinclude:: /_src/database/mongodb/demo/demo.js | ||
:language: javascript | ||
:lines: 2 | ||
|
||
*callback* 接收两个参数,*err* 和 *client* 。 | ||
*client* 表示已连接的 *MongoClient* 对象实例。 | ||
|
||
新建数据库 | ||
========== | ||
|
||
连上了数据库服务器,我们现在还没有任何数据库,因此需要先新建一个: | ||
|
||
.. literalinclude:: /_src/database/mongodb/demo/demo.js | ||
:language: javascript | ||
:lines: 6-8 | ||
:linenos: | ||
|
||
定义数据库名为 *user*,使用 *db* 方法建立数据库。 | ||
|
||
建表 | ||
==== | ||
|
||
在 *MongoDB* 中,表被称为 *collection*。建表,也即是建立 *collection* 。 | ||
|
||
.. literalinclude:: /_src/database/mongodb/demo/demo.js | ||
:language: javascript | ||
:lines: 9-11 | ||
:linenos: | ||
|
||
定义表名为 *user*,使用 *createCollection* 方法建表。 | ||
|
||
数据操作 | ||
======== | ||
|
||
插入 | ||
---- | ||
|
||
建表后,我们往 *user* 表中插入一条数据: | ||
|
||
.. literalinclude:: /_src/database/mongodb/demo/demo.js | ||
:language: javascript | ||
:lines: 13-22 | ||
:linenos: | ||
|
||
学过 *NeDB* 的同学就会发现,这样的数据操作跟 *NeDB* 的写法如出一辙。 | ||
实际上,*NeDB* 是 *MongoDB* 的简化版,因此在写法上延续了 *MongoDB* 的风格。 | ||
|
||
查询 | ||
---- | ||
|
||
查询 *Alice* 的数据: | ||
|
||
.. literalinclude:: /_src/database/mongodb/demo/demo.js | ||
:language: javascript | ||
:lines: 23-29 | ||
:linenos: | ||
|
||
*find* 方法并不会返回最终结果集,而是返回一个游标 *Cursor*。 | ||
要想获取全部结果,需要操作游标一个个寻找数据。 | ||
第4行,*toArray* 方法封装了这样的操作,并将数据组织成数组返回。 | ||
|
||
更新 | ||
---- | ||
|
||
将 *Alice* 的年龄更改为 *20* : | ||
|
||
.. literalinclude:: /_src/database/mongodb/demo/demo.js | ||
:language: javascript | ||
:lines: 30-43 | ||
:linenos: | ||
|
||
与 *updateOne* 对应的,有 *updateMany* 方法,用于更新多条数据。 | ||
|
||
验证下是否更改了 *Alice* 的数据: | ||
|
||
.. literalinclude:: /_src/database/mongodb/demo/demo.js | ||
:language: javascript | ||
:lines: 44-50 | ||
:linenos: | ||
|
||
删除 | ||
---- | ||
|
||
删除 *Alice* 的数据: | ||
|
||
.. literalinclude:: /_src/database/mongodb/demo/demo.js | ||
:language: javascript | ||
:lines: 51-60 | ||
:linenos: | ||
|
||
*deleteOne* 用于删除一条数据。如要删除多条数据,可以使用 *deleteMany* 方法。 | ||
|
||
关闭数据库 | ||
========== | ||
|
||
最后,使用 *close* 方法关闭数据库: | ||
|
||
.. literalinclude:: /_src/database/mongodb/demo/demo.js | ||
:language: javascript | ||
:lines: 61-68 | ||
:linenos: | ||
|
||
运行demo | ||
======== | ||
|
||
执行命令: | ||
|
||
.. code-block:: shell-session | ||
$ node demo.js | ||
终端输出如下结果: | ||
|
||
.. code-block:: text | ||
insert data: { n: 1, ok: 1 } | ||
update Alice data | ||
find Alice: [ { _id: 5dcd1195c792ec2f720e8dc7, name: 'Alice', age: 19 } ] | ||
find Alice new data: [ { _id: 5dcd1195c792ec2f720e8dc7, name: 'Alice', age: 20 } ] | ||
remove data | ||
close database | ||
下一步 | ||
====== | ||
|
||
.. include:: /_fragments/next-step-to-wechat-mp.rst | ||
|
||
.. include:: /_fragments/disqus.rst | ||
|
||
.. _mongodb模块: https://mongodb.github.io/node-mongodb-native/ | ||
.. _MongoDB: https://www.mongodb.com/ |
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
Oops, something went wrong.