2023-01-25 18:31:07 +00:00
|
|
|
---
|
|
|
|
slug: /en/sql-reference/table-functions/mongodb
|
|
|
|
sidebar_position: 42
|
|
|
|
sidebar_label: mongodb
|
|
|
|
---
|
|
|
|
|
|
|
|
# mongodb
|
|
|
|
|
|
|
|
Allows `SELECT` queries to be performed on data that is stored on a remote MongoDB server.
|
|
|
|
|
|
|
|
**Syntax**
|
|
|
|
|
|
|
|
``` sql
|
2023-01-25 18:44:13 +00:00
|
|
|
mongodb(host:port, database, collection, user, password, structure [, options])
|
2023-01-25 18:31:07 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
**Arguments**
|
|
|
|
|
2023-04-19 15:55:29 +00:00
|
|
|
- `host:port` — MongoDB server address.
|
2023-01-25 18:31:07 +00:00
|
|
|
|
2023-04-19 15:55:29 +00:00
|
|
|
- `database` — Remote database name.
|
2023-01-25 18:31:07 +00:00
|
|
|
|
2023-04-19 15:55:29 +00:00
|
|
|
- `collection` — Remote collection name.
|
2023-01-25 18:31:07 +00:00
|
|
|
|
2023-04-19 15:55:29 +00:00
|
|
|
- `user` — MongoDB user.
|
2023-01-25 18:31:07 +00:00
|
|
|
|
2023-04-19 15:55:29 +00:00
|
|
|
- `password` — User password.
|
2023-01-25 18:31:07 +00:00
|
|
|
|
2023-04-19 15:55:29 +00:00
|
|
|
- `structure` - The schema for the ClickHouse table returned from this function.
|
2023-01-25 18:31:07 +00:00
|
|
|
|
2023-04-19 15:55:29 +00:00
|
|
|
- `options` - MongoDB connection string options (optional parameter).
|
2023-01-25 18:31:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
**Returned Value**
|
|
|
|
|
|
|
|
A table object with the same columns as the original MongoDB table.
|
|
|
|
|
|
|
|
|
|
|
|
**Examples**
|
|
|
|
|
|
|
|
Suppose we have a collection named `my_collection` defined in a MongoDB database named `test`, and we insert a couple of documents:
|
|
|
|
|
|
|
|
```sql
|
|
|
|
db.createUser({user:"test_user",pwd:"password",roles:[{role:"readWrite",db:"test"}]})
|
|
|
|
|
|
|
|
db.createCollection("my_collection")
|
|
|
|
|
|
|
|
db.my_collection.insertOne(
|
|
|
|
{ log_type: "event", host: "120.5.33.9", command: "check-cpu-usage -w 75 -c 90" }
|
|
|
|
)
|
|
|
|
|
|
|
|
db.my_collection.insertOne(
|
|
|
|
{ log_type: "event", host: "120.5.33.4", command: "system-check"}
|
|
|
|
)
|
|
|
|
```
|
|
|
|
|
|
|
|
Let's query the collection using the `mongodb` table function:
|
|
|
|
|
|
|
|
```sql
|
|
|
|
SELECT * FROM mongodb(
|
|
|
|
'127.0.0.1:27017',
|
|
|
|
'test',
|
|
|
|
'my_collection',
|
|
|
|
'test_user',
|
|
|
|
'password',
|
|
|
|
'log_type String, host String, command String',
|
|
|
|
'connectTimeoutMS=10000'
|
|
|
|
)
|
|
|
|
```
|
|
|
|
|
|
|
|
**See Also**
|
|
|
|
|
2023-04-19 15:55:29 +00:00
|
|
|
- [The `MongoDB` table engine](/docs/en/engines/table-engines/integrations/mongodb.md)
|
|
|
|
- [Using MongoDB as a dictionary source](/docs/en/sql-reference/dictionaries/index.md#mongodb)
|