AI エージェント向け: ドキュメントインデックスは https://www.mongodb.com/ja-jp/docs/llms.txt で利用できます。すべてのページの markdown バージョンは、いずれかの URL パスに .md を追加することで利用できます。
Docs Menu

mongotranslate SQL 翻訳ツール

mongotranslate は、MongoDB 集計言語でSQLクエリを表現する方法をユーザーが理解できるように設計された学習ツールです。BI Connector のSQLから集計への変換エンジンは変換を提供します。これは、特定のBI Connector の変換の問題をトラブルシューティングするためにも使用できます。

mongotranslate is a stand-alone program. It requires a drdl file generated by mongodrdl. It doesn't require mongosqld or mongod to be running at the time of execution.

mongotranslate の構文は次のとおりです。

mongotranslate [--query | --queryFile] <query | queryfile> [options]
--query

Specifies a SQL query to translate into a MongoDB aggregation pipeline. Either --query or --queryFile is required.

--queryFile

Specifies a path to a file containing a SQL query to translate into a MongoDB aggregation pipeline. Either --query or --queryFile is required.

--schema

必須。 SQL クエリを集計パイプラインに変換するときに使用するmongodrdlプログラムによって作成された 1 つ以上の.drdl .drdlファイルを含むディレクトリを指定します。

--dbName

デフォルト: test

SQL クエリ内の非修飾テーブル名に使用するデータベース名。

次の例えでは、 fruitという名前のコレクションと--dbNameオプションを使用して、 fruitgroceriesデータベースにあることを指定します。

mongotranslate "SELECT * FROM fruit WHERE _id > 100;" \
--schema schema.drdl --dbName groceries

データベースを指定するために--dbNameオプションを使用しない場合、 mongotranslatefruittestデータベースにあると想定します。 スキーマにtestという名前のデータベース、またはtestデータベース内のテーブル名fruitが含まれていない場合、 mongotranslateはエラーを返します。

次の例では完全修飾テーブル名を使用しているため、 --dbNameオプションは必要ありません。

mongotranslate "SELECT * FROM groceries.fruit WHERE _id > 100;" \
--schema schema.drdl

SQL クエリで各テーブル名を持つデータベースを指定する場合、 --dbNameオプションを使用すると無視されます。

--explain

任意。 変換された集計パイプラインではなくクエリプランのexplain出力を返します。 --explainを使用すると、BI Connector に接続された BI ツールでEXPLAIN <query>を実行するのと同様の出力が返されます。

--format

デフォルト: multiline

任意。 有効なオプションはnonemultilineです。 デフォルトのmultilineオプションを使用すると、より簡単に読みやすい形式で結果が表示されます。

コマンドタイプ
オプション
説明

Without the --explain option

--format none

集計パイプラインすべてを 1 行で返します。

Without the --explain option

--format multiline

1 行につき 1 つのパイプライン ステージを持つ集計パイプラインを返します。

With the --explain option

--format none

すべてのフィールドを 1 行で返します。

With the --explain option

--format multiline

配列とオブジェクト サブフィールドの追加形式とともに、1 行に 1 つのフィールドを返します。

--mongoversion

デフォルト: latest

任意。 指定された MongoDB バージョンと互換性のある集計パイプラインを返します。 受け入れ可能な値は以下の通りです。

  • 3.2

  • 3.4

  • 3.6

  • 4.0

  • latest

デフォルト値は latest です。

次の例では、翻訳用のインライン クエリを指定しています。

mongotranslate --query=“select test.name from restaurants where name like 'Brooklyn%'” \
--schema=schema.drdl

上記のコマンドは、次の結果を返します。

[
{"$match": {"name": {"$regex": "^Brooklyn.*$","$options": "i"}}},
{"$project": {"test_DOT_restaurants_DOT_name": "$name","_id": {"$numberInt":"0"}}},
]

注意

To quote a string inside the --query parameter, use single quotes. If you must use double quotes or backticks in your query, use the --queryFile option.

次の例では、クエリ ファイルを指定し、1 行の結果を返します。

mongotranslate --queryFile=query.txt --schema=schema.drdl --format=none

The following example uses the --explain option:

mongotranslate --query="select count(name) from restaurants;" \
--schema=schema.drdl --explain

上記のコマンドは、次の結果を返します。

[
{
"ID": 1,
"StageType": "MongoSourceStage",
"Columns": "[{name: 'count(name)', type: 'int'}]",
"Sources": null,
"Database": {},
"Tables": {},
"Aliases": {},
"Collections": {},
"Pipeline": {},
"PipelineExplain": {},
"PushdownFailures": null
}
]