Skip to content

SQL Converter to Mongo Query

The MongoSqlQueryHolder provides a way to convert a SQL query to a Mongo query and execute it on a Mongo database.

Usage

Initialize the query holder with the SQL query you want to analyse or execute.

scala
val queryConverter = MongoSqlQueryHolder("select count(*) as anz from people where age < 30 and (age < 30 or age > 30) order by id asc")

In most cases you simply want to run the query and get the result as a Seq[Document].

TIP

The method run returns a classical MongoDb Observable use the implicits to convert it to a Seq[Document].

scala
val selectResponse = queryConverter.run(TestDatabase.provider).resultList()

You can also get the information about the collection and the keys that are used in the query.

scala
queryConverter.getCollection mustEqual "people"
scala
queryConverter.getKeysForEmptyDocument mustEqual Set("anz")

In some cases you need the information about the function calls in the query, for example in your own jdbc driver implementation. Because the difference of MongoDb and SQL for example a sql select count(*) from empty-collection is a list documents with one element and the MongoDb has no document in it.

scala
queryConverter.hasFunctionCallInSelect mustEqual true

Released under the Apache License 2.0.