2021-02-07 14:29:54 +00:00
---
2022-08-28 14:53:34 +00:00
slug: /en/engines/table-engines/integrations/mongodb
2023-06-23 13:16:22 +00:00
sidebar_position: 135
2022-04-09 13:29:05 +00:00
sidebar_label: MongoDB
2021-02-07 14:29:54 +00:00
---
2022-06-02 10:55:18 +00:00
# MongoDB
2021-02-07 14:29:54 +00:00
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**
2024-06-05 11:16:38 +00:00
- `host:port` — MongoDB server address.
2021-02-07 14:29:54 +00:00
2023-04-19 15:55:29 +00:00
- `database` — Remote database name.
2021-02-07 14:29:54 +00:00
2023-04-19 15:55:29 +00:00
- `collection` — Remote collection name.
2021-02-07 14:29:54 +00:00
2023-04-19 15:55:29 +00:00
- `user` — MongoDB user.
2021-02-07 14:29:54 +00:00
2023-04-19 15:55:29 +00:00
- `password` — User password.
2021-02-07 14:29:54 +00:00
2023-04-19 15:55:29 +00:00
- `options` — MongoDB connection string options (optional parameter).
2021-03-23 15:18:12 +00:00
2023-07-07 14:32:44 +00:00
:::tip
2024-06-05 01:24:57 +00:00
If you are using the MongoDB Atlas cloud offering:
2023-07-07 14:32:44 +00:00
```
2024-06-05 01:24:57 +00:00
- connection url can be obtained from 'Atlas SQL' option
- use options: 'connectTimeoutMS=10000& ssl=true& authSource=admin'
2023-07-07 14:32:44 +00:00
```
:::
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');
```