Skip to main content
Edit this page

mongodb

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

構文

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

引数

  • host:port — MongoDBサーバーのアドレス。

  • database — リモートデータベース名。

  • collection — リモートコレクション名。

  • user — MongoDBユーザー。

  • password — ユーザーパスワード。

  • structure - この関数から返されるClickHouseテーブルのスキーマ。

  • options - MongoDB接続文字列オプション(オプションのパラメータ)。

Tip

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

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

また、URIで接続することもできます:

mongodb(uri, collection, structure)

引数

  • uri — 接続文字列。

  • collection — リモートコレクション名。

  • structure — この関数から返されるClickHouseテーブルのスキーマ。

返される値

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

MongoDBデータベースtestに定義されているコレクション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:password@127.0.0.1:27017/test?connectionTimeoutMS=10000',
'my_collection',
'log_type String, host String, command String'
)

関連項目