> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-fix-nav-issues.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

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

# mongodb

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

<div id="syntax">
  ## 構文
</div>

```sql theme={null}
mongodb(host:port, database, collection, user, password, structure[, options[, oid_columns]]);
mongodb(uri, collection, structure[, oid_columns]);
mongodb(named_collection_name[, <arg>=<value>...]);
```

<div id="arguments">
  ## 引数
</div>

| 引数            | 説明                                            |
| ------------- | --------------------------------------------- |
| `host:port`   | MongoDB サーバーのアドレス。                            |
| `database`    | リモートデータベース名。                                  |
| `collection`  | リモートコレクション名。                                  |
| `user`        | MongoDB ユーザー。                                 |
| `password`    | ユーザーのパスワード。                                   |
| `structure`   | この関数が返す ClickHouse テーブルのスキーマ。                 |
| `options`     | MongoDB の接続文字列オプション (省略可能なパラメーター) 。           |
| `oid_columns` | WHERE 句で `oid` として扱うカラムのカンマ区切りリスト。既定値は `_id`。 |

<Tip>
  MongoDB Atlas クラウドを利用している場合は、次のオプションを追加してください。

  ```ini theme={null}
  'connectTimeoutMS=10000&ssl=true&authSource=admin'
  ```
</Tip>

URI を使って接続することもできます。

```sql theme={null}
mongodb(uri, collection, structure[, oid_columns])
```

| 引数            | 説明                                               |
| ------------- | ------------------------------------------------ |
| `uri`         | 接続文字列。                                           |
| `collection`  | リモートコレクションの名前。                                   |
| `structure`   | この関数が返す ClickHouse テーブルのスキーマ。                    |
| `oid_columns` | WHERE 句内で `oid` として扱うカラムのカンマ区切りリスト。デフォルトは `_id`。 |
| :::           |                                                  |

名前付きコレクションを使用して引数を渡すこともできます:

```sql theme={null}
mongodb(_named_collection_[, host][, port][, database][, collection][, user][, password][, structure][, options][, oid_columns])
-- または
mongodb(_named_collection_[, uri][, structure][, oid_columns])
```

<div id="returned_value">
  ## 戻り値
</div>

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

<div id="examples">
  ## 例
</div>

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

```sql theme={null}
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`テーブル関数を使って、コレクションにクエリを実行してみましょう。

```sql theme={null}
SELECT * FROM mongodb(
    '127.0.0.1:27017',
    'test',
    'my_collection',
    'test_user',
    'password',
    'log_type String, host String, command String',
    'connectTimeoutMS=10000'
)
```

または:

```sql theme={null}
SELECT * FROM mongodb(
    'mongodb://test_user:password@127.0.0.1:27017/test?connectionTimeoutMS=10000',
    'my_collection',
    'log_type String, host String, command String'
)
```

または:

```sql theme={null}
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'
)
```

<div id="related">
  ## 関連
</div>

* [`MongoDB` テーブルエンジン](/ja/reference/engines/table-engines/integrations/mongodb)
* [MongoDB を Dictionary ソースとして使う](/ja/reference/statements/create/dictionary/sources/mongodb)
