2021-06-12 18:07:29 +00:00
|
|
|
---
|
2022-08-26 19:07:59 +00:00
|
|
|
slug: /zh/engines/table-engines/integrations/mongodb
|
2022-04-10 23:08:18 +00:00
|
|
|
sidebar_position: 5
|
|
|
|
sidebar_label: MongoDB
|
2021-06-12 18:07:29 +00:00
|
|
|
---
|
|
|
|
|
|
|
|
# MongoDB {#mongodb}
|
|
|
|
|
|
|
|
MongoDB 引擎是只读表引擎,允许从远程 MongoDB 集合中读取数据(`SELECT`查询)。引擎只支持非嵌套的数据类型。不支持 `INSERT` 查询。
|
|
|
|
|
|
|
|
## 创建一张表 {#creating-a-table}
|
|
|
|
|
|
|
|
``` sql
|
|
|
|
CREATE TABLE [IF NOT EXISTS] [db.]table_name
|
|
|
|
(
|
|
|
|
name1 [type1],
|
|
|
|
name2 [type2],
|
|
|
|
...
|
|
|
|
) ENGINE = MongoDB(host:port, database, collection, user, password);
|
|
|
|
```
|
|
|
|
|
|
|
|
**引擎参数**
|
|
|
|
|
|
|
|
- `host:port` — MongoDB 服务器地址.
|
|
|
|
|
|
|
|
- `database` — 数据库名称.
|
|
|
|
|
|
|
|
- `collection` — 集合名称.
|
|
|
|
|
|
|
|
- `user` — MongoDB 用户.
|
|
|
|
|
|
|
|
- `password` — 用户密码.
|
|
|
|
|
|
|
|
## 用法示例 {#usage-example}
|
|
|
|
|
|
|
|
ClickHouse 中的表,从 MongoDB 集合中读取数据:
|
|
|
|
|
|
|
|
``` text
|
|
|
|
CREATE TABLE mongo_table
|
|
|
|
(
|
2021-07-29 15:20:55 +00:00
|
|
|
key UInt64,
|
2021-06-12 18:07:29 +00:00
|
|
|
data String
|
|
|
|
) ENGINE = MongoDB('mongo1:27017', 'test', 'simple_table', 'testuser', 'clickhouse');
|
|
|
|
```
|
|
|
|
|
|
|
|
查询:
|
|
|
|
|
|
|
|
``` sql
|
|
|
|
SELECT COUNT() FROM mongo_table;
|
|
|
|
```
|
|
|
|
|
|
|
|
``` text
|
|
|
|
┌─count()─┐
|
|
|
|
│ 4 │
|
|
|
|
└─────────┘
|
|
|
|
```
|
|
|
|
|
2021-09-19 20:05:54 +00:00
|
|
|
[原始文章](https://clickhouse.com/docs/en/engines/table-engines/integrations/mongodb/) <!--hide-->
|