A robust go package that converts SQL queries to MongoDB aggregation pipelines.
Download cli from here Click here
Zero-SQL supports a comprehensive set of SQL features:
- SELECT statements with column selection and aliases
- FROM clauses with table references
- JOIN operations (INNER, LEFT, RIGHT)
- WHERE clauses with complex conditions (AND, OR, nested conditions)
- Comparison operators (=, !=, >, <, >=, <=, LIKE, ILIKE, IN, NOT IN)
- NULL checks (IS NULL, IS NOT NULL)
- ORDER BY clauses with ASC/DESC
- LIMIT and OFFSET
- GROUP BY with aggregation functions (COUNT, SUM, AVG, MIN, MAX)
- HAVING clauses
git clone https://github.com/synhq/zero-sql
cd zero-sql
go build -o zero-sql buildgo install github.com/synhq/zero-sqlzero-sql "SELECT name, age FROM users WHERE age > 18"Usage:
zero-sql [SQL_QUERY] [flags]
Flags:
-f, --format string Output format (json, bson) (default "json")
-h, --help help for zero-sql
-p, --pretty Pretty print the output (default true)
-v, --verbose Verbose outputzero-sql "SELECT name, email FROM users WHERE active = true"Output:
[
{
"$match": {
"active": true
}
},
{
"$project": {
"_id": 0,
"name": "$name",
"email": "$email"
}
}
]zero-sql "SELECT u.name, p.title FROM users u JOIN posts p ON u.id = p.user_id"Output:
[
{
"$lookup": {
"from": "posts",
"localField": "id",
"foreignField": "user_id",
"as": "p"
}
},
{
"$unwind": "$p"
},
{
"$project": {
"_id": 0,
"name": "$u.name",
"title": "$p.title"
}
}
]zero-sql "SELECT u.name, p.title FROM users u LEFT JOIN posts p ON u.id = p.user_id"Output:
[
{
"$lookup": {
"from": "posts",
"localField": "id",
"foreignField": "user_id",
"as": "p"
}
},
{
"$unwind": {
"path": "$p",
"preserveNullAndEmptyArrays": true
}
},
{
"$project": {
"_id": 0,
"name": "$u.name",
"title": "$p.title"
}
}
]zero-sql "SELECT u.name, p.title, c.name as category FROM users u JOIN posts p ON u.id = p.user_id JOIN categories c ON p.category_id = c.id"Output:
[
{
"$lookup": {
"from": "posts",
"localField": "id",
"foreignField": "user_id",
"as": "p"
}
},
{
"$unwind": "$p"
},
{
"$lookup": {
"from": "categories",
"localField": "category_id",
"foreignField": "id",
"as": "c"
}
},
{
"$unwind": "$c"
},
{
"$project": {
"_id": 0,
"category": "$c.name",
"name": "$u.name",
"title": "$p.title"
}
}
]zero-sql "SELECT status, COUNT(*) as total FROM orders GROUP BY status"Output:
[
{
"$group": {
"_id": {
"status": "$status"
},
"total": {
"$sum": 1
}
}
}
]zero-sql "SELECT * FROM products WHERE (price > 100 AND category = 'electronics') OR (price < 50 AND category = 'books')"zero-sql "SELECT name FROM users WHERE email ILIKE '%@gmail.com'"zero-sql "SELECT name, created_at FROM users ORDER BY created_at DESC LIMIT 10"- Column selection:
SELECT name, age - Wildcard:
SELECT * - Column aliases:
SELECT name AS full_name - Aggregation functions:
COUNT(),SUM(),AVG(),MIN(),MAX()
- Table references:
FROM users - Table aliases:
FROM users u
- INNER JOIN:
INNER JOIN posts ON users.id = posts.user_id - LEFT JOIN:
LEFT JOIN posts ON users.id = posts.user_id - Table aliases in JOINs:
FROM users u JOIN posts p ON u.id = p.user_id - Multiple JOINs:
FROM users u JOIN posts p ON u.id = p.user_id JOIN categories c ON p.category_id = c.id - JOINs with WHERE conditions:
FROM users u JOIN posts p ON u.id = p.user_id WHERE u.active = true
- Comparison operators:
=,!=,<>,>,<,>=,<= - Pattern matching:
LIKE,ILIKE - List operations:
IN,NOT IN - NULL checks:
IS NULL,IS NOT NULL - Logical operators:
AND,OR,NOT - Parentheses for grouping:
(condition1 OR condition2) AND condition3
- Single column:
GROUP BY status - Multiple columns:
GROUP BY category, status - Works with aggregation functions in SELECT
- Filter aggregated results:
HAVING COUNT(*) > 5 - Supports same operators as WHERE clause
- Ascending:
ORDER BY nameorORDER BY name ASC - Descending:
ORDER BY created_at DESC - Multiple columns:
ORDER BY category, name DESC
- Limit results:
LIMIT 10 - Skip results:
OFFSET 20orLIMIT 20, 10
The tool generates MongoDB aggregation pipelines using these stages:
$lookup- for JOIN operations$unwind- to flatten joined arrays$match- for WHERE and HAVING clauses$group- for GROUP BY clauses$project- for SELECT column specification$sort- for ORDER BY clauses$skip- for OFFSET$limit- for LIMIT
Zero-SQL provides detailed error messages for:
- Invalid SQL syntax
- Unsupported SQL features
- Type mismatches
- Missing required clauses
Use the --verbose flag for additional debugging information.
Current limitations include:
- Only SELECT statements are supported
- Subqueries are not yet supported
- Window functions are not supported
- Some advanced SQL features may not be available
Contributions are welcome! Please see the Contributing Guide for details.
This project is licensed under the MIT License - see the LICENSE file for details.
The project follows a clean architecture with clear separation of concerns:
zero-sql/
├── cmd/ # CLI command definitions
│ └── root.go # Main command setup
├── internal/converter/ # Core conversion logic
│ ├── converter.go # Main converter with pipeline building
│ ├── parser.go # SQL AST parsing helpers
│ └── operators.go # SQL to MongoDB operator mappings
├── main.go # Application entry point
├── go.mod # Go module definition
└── README.md # This file
The converter package is the heart of the application, handling the conversion from SQL Abstract Syntax Trees (AST) to MongoDB aggregation pipelines.
