QueryLeaf is available for Node.js, as a cli, and as a standalone webserver
SELECT u.name, COUNT(o._id) as order_count
FROM users u
LEFT JOIN orders o ON u._id = o.userId
WHERE u.status = 'active'
AND o.items[0].price > 100
GROUP BY u.name
ORDER BY order_count DESC
LIMIT 5 Write in SQL, run on MongoDB with zero translation hassle
QueryLeaf is a PostgreSQL dialect SQL layer for MongoDB that lets your team use familiar SQL without compromising on MongoDB's power.
Easily access nested fields, arrays, and complex document structures with a SQL syntax that understands MongoDB's document model.
Use as a library in your code, run the CLI for terminal access, or launch the web server for a MongoDB SQL proxy - flexible options for every workflow.
Works with your existing MongoDB client instances — no proxies, no middleware, no separate services. Minimal overhead, maximum compatibility.
Write familiar PostgreSQL syntax while QueryLeaf automatically handles nested fields, array elements, and MongoDB ObjectIDs
db.collection('users').aggregate([
{
$lookup: {
from: "orders",
localField: "_id",
foreignField: "userId",
as: "orders"
}
},
{ $unwind: { path: "$orders", preserveNullAndEmptyArrays: true } },
{
$match: {
'orders.items.0.price': { $gt: 100 },
'orders.shipping.address.city': 'Chicago'
}
},
{
$project: {
name: 1,
total: "$orders.total",
status: "$orders.status"
}
}
])
Choose the right option for your workflow
import { QueryLeaf } from '@queryleaf/lib';
import { MongoClient } from 'mongodb';
// Your existing MongoDB client
const mongoClient = new MongoClient('mongodb://localhost:27017');
await mongoClient.connect();
// Create QueryLeaf with your MongoDB client
const queryLeaf = new QueryLeaf(mongoClient, 'mydatabase');
// Execute SQL queries against MongoDB
const results = await queryLeaf.execute(`
SELECT u.name, u.email, COUNT(o._id) as order_count
FROM users u
LEFT JOIN orders o ON u._id = o.userId
WHERE u.status = 'active'
GROUP BY u.name, u.email
ORDER BY order_count DESC
LIMIT 10
`);
// Regular MongoDB operations still work normally
const db = mongoClient.db('mydatabase');
await db.collection('logs').insertOne({
event: 'query_executed',
timestamp: new Date()
});
# Install globally
npm install -g @queryleaf/cli
# Execute a query
queryleaf --db mydb --query "SELECT * FROM users WHERE age > 21"
# Interactive mode
queryleaf --db mydb --interactive
sql> SELECT name, email FROM users LIMIT 5;
name | email | age
----------+----------------------+-----------
John Doe | john@example.com | 30
Jane Smith| jane@example.com | 25
...
sql> .tables
Collections in database:
users
products
orders
# Install globally
npm install -g @queryleaf/postgres-server
# Start the PostgreSQL-compatible server
queryleaf-pg-server --db mydb
# Connect with any PostgreSQL client:
psql -h localhost -p 5432 -d mydb -U any_username
# Or use in your application code:
import { MongoClient } from 'mongodb';
import { PostgresServer } from '@queryleaf/postgres-server';
// Create and start the server
const mongoClient = new MongoClient('mongodb://localhost:27017');
await mongoClient.connect();
const pgServer = new PostgresServer(mongoClient, 'mydb', {
port: 5432,
host: 'localhost'
});
Join teams that use QueryLeaf to simplify MongoDB development without sacrificing document database capabilities