メインコンテンツまでスキップ
メインコンテンツまでスキップ

mongodb テーブル関数

リモートの MongoDB サーバーに保存されているデータに対して SELECT クエリを実行できるようにします。

構文

mongodb(host:port, database, collection, user, password, structure[, options[, oid_columns]])

引数

引数説明
host:portMongoDB サーバーのアドレス。
databaseリモートデータベースの名前。
collectionリモートコレクションの名前。
userMongoDB ユーザー。
passwordユーザーパスワード。
structureこの関数から返される ClickHouse テーブルのスキーマ。
optionsMongoDB 接続文字列オプション(任意のパラメータ)。
oid_columnsWHERE 句で oid として扱うべきカラムのカンマ区切りリスト。デフォルトでは _id
ヒント

MongoDB Atlas クラウドオファリングを使用している場合は、次のオプションを追加してください:

'connectTimeoutMS=10000&ssl=true&authSource=admin'

URI による接続も可能です:

mongodb(uri, collection, structure[, oid_columns])
引数説明
uri接続文字列。
collectionリモートコレクションの名前。
structureこの関数から返される ClickHouse テーブルのスキーマ。
oid_columnsWHERE 句で oid として扱うべきカラムのカンマ区切りリスト。デフォルトでは _id

戻り値

元の MongoDB テーブルと同じカラムを持つテーブルオブジェクト。

test という MongoDB データベースに my_collection というコレクションが定義されているとしましょう。いくつかのドキュメントを挿入します:

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:[email protected]:27017/test?connectionTimeoutMS=10000',
    'my_collection',
    'log_type String, host String, command String'
)