SELECT 查询。
语法
mongodb(host:port, database, collection, user, password, structure[, options[, oid_columns]]);
mongodb(uri, collection, structure[, oid_columns]);
mongodb(named_collection_name[, <arg>=<value>...]);
参数
| Argument | Description |
|---|---|
host:port | MongoDB server 地址。 |
database | 远程数据库名称。 |
collection | 远程集合名称。 |
user | MongoDB 用户。 |
password | 用户密码。 |
structure | 此函数返回的 ClickHouse 表的 schema。 |
options | MongoDB connection string 选项 (可选参数) 。 |
oid_columns | 在 WHERE 子句中应视为 oid 的列的逗号分隔列表。默认为 _id。 |
如果你使用的是 MongoDB Atlas 云服务,请添加以下选项:
'connectTimeoutMS=10000&ssl=true&authSource=admin'
mongodb(uri, collection, structure[, oid_columns])
| 参数 | 描述 |
|---|---|
uri | 连接字符串。 |
collection | 远程集合名称。 |
structure | 此函数返回的 ClickHouse 表的 schema。 |
oid_columns | 在 WHERE 子句中应视为 oid 的以逗号分隔的列列表。默认为 _id。 |
| ::: |
mongodb(_named_collection_[, host][, port][, database][, collection][, user][, password][, structure][, options][, oid_columns])
-- 或
mongodb(_named_collection_[, uri][, structure][, oid_columns])
返回值
示例
my_collection 的集合,位于名为 test 的 MongoDB 数据库中,并插入了几条文档:
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"}
)
mongodb 表函数查询该集合:
SELECT * FROM mongodb(
'127.0.0.1:27017',
'test',
'my_collection',
'test_user',
'password',
'log_type String, host String, command String',
'connectTimeoutMS=10000'
)
SELECT * FROM mongodb(
'mongodb://test_user:password@127.0.0.1:27017/test?connectionTimeoutMS=10000',
'my_collection',
'log_type String, host String, command String'
)
CREATE NAMED COLLECTION mongo_creds AS
uri='mongodb://test_user:password@127.0.0.1:27017/test?connectionTimeoutMS=10000',
collection='default_collection';
SELECT * FROM mongodb(
mongo_creds,
collection = 'my_collection',
structure = 'log_type String, host String, command String'
)