Description
mongotranslate is a learning tool designed to help users understand
how SQL queries can be expressed in the MongoDB aggregation
language. The BI Connector's SQL-to-aggregation
translation engine provides the translations, which can also be used to
troubleshoot specific BI Connector translation issues.
mongotranslate is a standalone program. It requires
a drdl file generated by
mongodrdl, but does not require
mongosqld or mongod to be running at the
time of execution.
Usage
mongotranslate has the following syntax:
mongotranslate [--query | --queryFile] <query | queryfile> [options]
Command Line 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.
--schemaRequired. Specifies a
.drdlschema file or a directory containing one or more.drdlschema files created by themongodrdlprogram to use when translating a SQL query into an aggregation pipeline.
--dbNameDefault:
testThe database name to use for unqualified table names in the SQL query.
The following example uses a collection named
fruitand the--dbNameoption to specify thatfruitis in thegroceriesdatabase:mongotranslate "SELECT * FROM fruit WHERE _id > 100;" \ --schema schema.drdl --dbName groceries If you do not use the
--dbNameoption to specify a database,mongotranslateassumes thatfruitis in thetestdatabase. If the schema does not contain a database namedtest, or a table namefruitin thetestdatabase,mongotranslatereturns an error.The following example uses a fully-qualified table name, so it does not need the
--dbNameoption.mongotranslate "SELECT * FROM groceries.fruit WHERE _id > 100;" \ --schema schema.drdl If you specify a database with each table name in your SQL query, the
--dbNameoption is ignored if it is used.
--explainOptional. Returns the
explainoutput for the query plan instead of the translated aggregation pipeline. Using--explainreturns similar output to runningEXPLAIN <query>on a BI tool connected to BI Connector.
--formatDefault:
multilineOptional. Valid options are
noneandmultiline. The defaultmultilineoption displays results in a more easily readable format.Command TypeOptionDescriptionWithout the
--explainoption--format noneReturns an aggregation pipeline all on one line.
Without the
--explainoption--format multilineReturns an aggregation pipeline with one pipeline stage per line.
With the
--explainoption--format noneReturns all fields on one line.
With the
--explainoption--format multilineReturns one field per line, with additional formatting for arrays and object subfields.
Examples
The following example specifies an inline query for translation:
mongotranslate --query=“select test.name from restaurants where name like 'Brooklyn%'” \ --schema=schema.drdl
The above command returns the following results:
[ {"$match": {"name": {"$regex": "^Brooklyn.*$","$options": "i"}}}, {"$project": {"test_DOT_restaurants_DOT_name": "$name","_id": {"$numberInt":"0"}}}, ]
Note
If you need to quote a string inside the --query parameter, be sure to use single quotes.
If you must use double quotes or backticks in your query, use the
--queryFile option.
The following example specifies a query file and returns a one-line result:
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
The above command returns the following results:
[ { "ID": 1, "StageType": "MongoSourceStage", "Columns": "[{name: 'count(name)', type: 'int'}]", "Sources": null, "Database": {}, "Tables": {}, "Aliases": {}, "Collections": {}, "Pipeline": {}, "PipelineExplain": {}, "PushdownFailures": null } ]