説明
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]
コマンドライン オプション
--querySpecifies a SQL query to translate into a MongoDB aggregation pipeline. Either
--queryor--queryFileis required.
--queryFileSpecifies a path to a file containing a SQL query to translate into a MongoDB aggregation pipeline. Either
--queryor--queryFileis required.
--schema必須。 SQL クエリを集計パイプラインに変換するときに使用する
mongodrdlプログラムによって作成された 1 つ以上の.drdl.drdlファイルを含むディレクトリを指定します。
--dbNameデフォルト:
testSQL クエリ内の非修飾テーブル名に使用するデータベース名。
次の例えでは、
fruitという名前のコレクションと--dbNameオプションを使用して、fruitがgroceriesデータベースにあることを指定します。mongotranslate "SELECT * FROM fruit WHERE _id > 100;" \ --schema schema.drdl --dbName groceries データベースを指定するために
--dbNameオプションを使用しない場合、mongotranslateはfruitがtestデータベースにあると想定します。 スキーマに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任意。 有効なオプションは
noneとmultilineです。 デフォルトのmultilineオプションを使用すると、より簡単に読みやすい形式で結果が表示されます。コマンドタイプオプション説明Without the
--explainoption--format none集計パイプラインすべてを 1 行で返します。
Without the
--explainoption--format multiline1 行につき 1 つのパイプライン ステージを持つ集計パイプラインを返します。
With the
--explainoption--format noneすべてのフィールドを 1 行で返します。
With the
--explainoption--format multiline配列とオブジェクト サブフィールドの追加形式とともに、1 行に 1 つのフィールドを返します。
例
次の例では、翻訳用のインライン クエリを指定しています。
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 } ]