Skip to main content

A library for converting SQL queries to MongoDB aggregation pipelines

Project description

mongo-sql-parser

A Python library for converting SQL queries to MongoDB aggregation pipelines. This library provides comprehensive support for SQL-to-MongoDB query translation, including WHERE clauses, JOINs, GROUP BY, aggregations, and subqueries.

Features

  • SQL to MongoDB Conversion: Convert SQL SELECT queries to MongoDB aggregation pipelines
  • WHERE Clause Parsing: Parse complex SQL WHERE conditions into MongoDB filters
  • JOIN Support: Convert SQL JOINs to MongoDB $lookup operations
  • Aggregation Support: Support for GROUP BY, COUNT, SUM, AVG, MIN, MAX
  • Subquery Support: Handle EXISTS, IN, NOT IN, and comparison subqueries
  • MongoDB Shell Parser: Parse MongoDB shell expressions to Python dictionaries
  • MongoDB Query Validator: Validate MongoDB shell queries for security

Installation

pip install mongo-sql-parser

Quick Start

Convert SQL to MongoDB Pipeline

from mongo_sql_parser import MongoQueryParser

parser = MongoQueryParser()
sql = "SELECT name, age FROM users WHERE age > 25 AND city = 'NYC'"
pipeline = parser.parse_query(sql)
print(pipeline)
# Output: [
#   {'$match': {'age': {'$gt': 25}, 'city': 'NYC'}},
#   {'$project': {'_id': 0, 'name': 1, 'age': 1}}
# ]

Parse WHERE Conditions

from mongo_sql_parser import MongoFilterParser

parser = MongoFilterParser()
condition = "age > 25 AND (city = 'NYC' OR city = 'LA')"
filter_dict = parser.parse(condition)
print(filter_dict)
# Output: {'$and': [{'age': {'$gt': 25}}, {'$or': [{'city': 'NYC'}, {'city': 'LA'}]}]}

Parse MongoDB Shell Queries

from mongo_sql_parser import MongoShellParser

parser = MongoShellParser()
query = 'db.users.find({"age": {"$gt": 25}})'
result = parser.parse(query)
print(result)
# Output: {
#   'db_name': None,
#   'collection_name': 'users',
#   'function': 'find',
#   'expression': {'age': {'$gt': 25}}
# }

Supported SQL Features

  • SELECT: Projection of fields
  • WHERE: Filter conditions with support for:
    • Comparison operators (=, !=, >, <, >=, <=)
    • Logical operators (AND, OR, NOT)
    • LIKE / ILIKE pattern matching
    • IN / NOT IN clauses
    • IS NULL / IS NOT NULL
    • BETWEEN ranges
  • JOIN: INNER, LEFT, RIGHT, and CROSS JOINs
  • GROUP BY: Field grouping with aggregations
  • HAVING: Post-aggregation filtering
  • ORDER BY: Sorting
  • LIMIT: Result limiting
  • Subqueries: EXISTS, IN, NOT IN, comparison subqueries

API Reference

MongoQueryParser

Main class for parsing SQL queries into MongoDB pipelines.

parser = MongoQueryParser()
pipeline = parser.parse_query(sql_string)

MongoFilterParser

Parses SQL WHERE conditions into MongoDB filters.

parser = MongoFilterParser()
filter_dict = parser.parse(condition_string)

MongoShellParser

Parses MongoDB shell queries to extract database, collection, and expression information.

parser = MongoShellParser()
result = parser.parse(shell_query_string)

Requirements

  • Python 3.7+
  • sqlglot >= 10.0.0

License

MIT License

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Changelog

0.1.0 (Initial Release)

  • Initial release with SQL to MongoDB conversion support
  • WHERE clause parsing
  • JOIN support
  • Aggregation support
  • Subquery handling

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mongo_sql_parser-0.1.0.tar.gz (34.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mongo_sql_parser-0.1.0-py3-none-any.whl (34.0 kB view details)

Uploaded Python 3

File details

Details for the file mongo_sql_parser-0.1.0.tar.gz.

File metadata

  • Download URL: mongo_sql_parser-0.1.0.tar.gz
  • Upload date:
  • Size: 34.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.3

File hashes

Hashes for mongo_sql_parser-0.1.0.tar.gz
Algorithm Hash digest
SHA256 7193a962681d9a874a4136c79bda16eecb270282dbaa6696476b21cfe5c7cc9e
MD5 28bc6748d1f9c4a297174976530f72cc
BLAKE2b-256 f274d97bdcb63cf6fa095168b44df0db898dc5911dd72fe02c246799d282cf5f

See more details on using hashes here.

File details

Details for the file mongo_sql_parser-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for mongo_sql_parser-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 799ca9bf7361b6833dde1871b56e71a752978fa12e75b8c3ca2eee274852cb67
MD5 7a1e9c6e5a79ac60c78550e82ed99e61
BLAKE2b-256 4ccf8ddad1b2b1a8d1ebdec83e952847360034083f5dbb94db5e7a94016b660f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page