2021-02-07 14:29:54 +00:00
---
2022-04-03 23:05:35 +00:00
toc_priority: 5
toc_title: MongoDB
2021-02-07 14:29:54 +00:00
---
# MongoDB {#mongodb}
2021-02-08 14:30:15 +00:00
MongoDB engine is read-only table engine which allows to read data (`SELECT` queries) from remote MongoDB collection. Engine supports only non-nested data types. `INSERT` queries are not supported.
2021-02-07 14:29:54 +00:00
## Creating a Table {#creating-a-table}
``` sql
CREATE TABLE [IF NOT EXISTS] [db.]table_name
(
name1 [type1],
name2 [type2],
...
2021-03-23 15:18:12 +00:00
) ENGINE = MongoDB(host:port, database, collection, user, password [, options]);
2021-02-07 14:29:54 +00:00
```
**Engine Parameters**
- `host:port` — MongoDB server address.
- `database` — Remote database name.
2021-02-08 14:30:15 +00:00
- `collection` — Remote collection name.
2021-02-07 14:29:54 +00:00
- `user` — MongoDB user.
- `password` — User password.
2021-03-23 15:18:12 +00:00
- `options` — MongoDB connection string options (optional parameter).
2021-02-07 14:29:54 +00:00
## Usage Example {#usage-example}
2021-03-23 15:18:12 +00:00
Create a table in ClickHouse which allows to read data from MongoDB collection:
2021-02-07 14:29:54 +00:00
2021-09-07 17:07:27 +00:00
``` sql
2021-02-07 14:29:54 +00:00
CREATE TABLE mongo_table
(
2021-03-23 15:18:12 +00:00
key UInt64,
2021-02-07 14:29:54 +00:00
data String
2021-02-08 14:30:15 +00:00
) ENGINE = MongoDB('mongo1:27017', 'test', 'simple_table', 'testuser', 'clickhouse');
2021-02-07 14:29:54 +00:00
```
2021-03-23 15:18:12 +00:00
To read from an SSL secured MongoDB server:
2021-09-07 17:07:27 +00:00
``` sql
2021-03-23 15:18:12 +00:00
CREATE TABLE mongo_table_ssl
(
key UInt64,
data String
) ENGINE = MongoDB('mongo2:27017', 'test', 'simple_table', 'testuser', 'clickhouse', 'ssl=true');
```
2021-02-07 14:29:54 +00:00
Query:
``` sql
SELECT COUNT() FROM mongo_table;
```
``` text
┌─count()─┐
│ 4 │
└─────────┘
```
2022-01-13 06:37:57 +00:00
You can also adjust connection timeout:
``` sql
CREATE TABLE mongo_table
(
key UInt64,
data String
) ENGINE = MongoDB('mongo2:27017', 'test', 'simple_table', 'testuser', 'clickhouse', 'connectTimeoutMS=100000');
```
2021-09-19 20:05:54 +00:00
[Original article ](https://clickhouse.com/docs/en/engines/table-engines/integrations/mongodb/ ) <!--hide-->