SQL to MongoDB Query Converter

repository·master·Indexed 43 days ago

https://github.com/vincentrussell/sql-to-mongo-db-query-converter

A Java library and standalone CLI tool that converts SQL statements into MongoDB queries. It supports SELECT, UPDATE, and DELETE statements, including complex aggregations (GROUP BY, HAVING), INNER JOINs via $lookup, pagination (LIMIT, OFFSET), and specialized functions for ObjectIds and date handling. Version 1.23 is available via Maven for projects using JDK 1.8 or higher.

Tokens
3.5K
Snippets
17
Records
20
Agent score
6%

What's inside sql-to-mongo-db-query-converter

  1. Run queries against a live MongoDB database

    master

    The converter allows you to execute the converted queries directly against a running MongoDB instance. You can specify the host, database, and batch size using the following flags:

    • -h: Host and port (e.g., localhost:3086)
    • -db: Database name
    • -b: Batch size (default is 50)

    When running in interactive mode with these flags, the tool will prompt you for SQL, execute it, and then ask if you want to see more results if the batch size is exceeded.

    java -jar target/sql-to-mongo-db-query-converter-1.18-SNAPSHOT-standalone.jar -i -h localhost:3086 -db local -b 5
  2. SQL to MongoDB: Regex and Pattern Matching

    master

    You can use regexMatch and notRegexMatch for regular expressions, or the LIKE operator for simple pattern matching.

    Regex Match: select * from my_table where regexMatch(column,'^[ae"gaf]+$')

    NOT Regex Match: select * from my_table where notRegexMatch(column,'^[ae"gaf]+$')

    LIKE (Start with): select * from my_table where value LIKE 'start%'

    NOT LIKE: select * from my_table where value NOT LIKE 'start%'

  3. SQL to MongoDB: Aggregation (GROUP BY and HAVING)

    master

    Complex SQL aggregations including GROUP BY, ORDER BY, and HAVING are converted into MongoDB .aggregate() pipelines using $match, $group, $sort, and $project stages.

    -- Group By with Order By
    select borough, cuisine, count(*) from my_collection WHERE borough LIKE 'Queens%' GROUP BY borough, cuisine ORDER BY count(*) DESC;
    
    -- Having clause
    select Restaurant.cuisine, count(*) from Restaurants group by Restaurant.cuisine having count(*) > 3;
    
    -- Count without GROUP BY
    select count(*) as c from table