Skip to content

Commit 152276a

Browse files
Peng RenCopilot
andcommitted
Update README
Co-authored-by: Copilot <copilot@github.com>
1 parent 44953ee commit 152276a

1 file changed

Lines changed: 61 additions & 14 deletions

File tree

README.md

Lines changed: 61 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ PyMongoSQL implements the DB API 2.0 interfaces to provide SQL-like access to Mo
2828
- **PartiQL-based SQL Syntax**: Built on [PartiQL](https://partiql.org/tutorial.html) (SQL for semi-structured data), enabling seamless SQL querying of nested and hierarchical MongoDB documents
2929
- **Nested Structure Support**: Query and filter deeply nested fields and arrays within MongoDB documents using standard SQL syntax
3030
- **MongoDB Aggregate Pipeline Support**: Execute native MongoDB aggregation pipelines using SQL-like syntax with `aggregate()` function
31+
- **SQL Aggregate Functions**: `COUNT(*)`, `SUM`, `AVG`, `MIN`, `MAX` translated to MongoDB aggregation pipelines
3132
- **SQLAlchemy Integration**: Complete ORM and Core support with dedicated MongoDB dialect
3233
- **SQL Query Support**: SELECT statements with WHERE conditions, field selection, and aliases
3334
- **DML Support**: Full support for INSERT, UPDATE, and DELETE operations using PartiQL syntax
@@ -87,6 +88,7 @@ pip install -e .
8788
- [WHERE Clauses](#where-clauses)
8889
- [Nested Field Support](#nested-field-support)
8990
- [Sorting and Limiting](#sorting-and-limiting)
91+
- [SQL Aggregate Functions](#sql-aggregate-functions)
9092
- [MongoDB Aggregate Function](#mongodb-aggregate-function)
9193
- [INSERT Statements](#insert-statements)
9294
- [UPDATE Statements](#update-statements)
@@ -293,6 +295,42 @@ Both functions:
293295
- **LIMIT**: `LIMIT 10`
294296
- **Combined**: `ORDER BY created_at DESC LIMIT 5`
295297

298+
### SQL Aggregate Functions
299+
300+
PyMongoSQL supports standard SQL aggregate functions that are automatically translated into MongoDB aggregation pipelines.
301+
302+
**Supported Functions**: `COUNT(*)`, `SUM(field)`, `AVG(field)`, `MIN(field)`, `MAX(field)`
303+
304+
**Basic Count**
305+
306+
```python
307+
cursor.execute("SELECT COUNT(*) AS total FROM users")
308+
row = cursor.fetchone()
309+
print(f"Total users: {row[0]}")
310+
```
311+
312+
**Multiple Aggregates**
313+
314+
```python
315+
cursor.execute(
316+
"SELECT COUNT(*) AS cnt, AVG(price) AS avg_price, MIN(price) AS cheapest, MAX(price) AS priciest FROM products"
317+
)
318+
```
319+
320+
**Aggregate with WHERE**
321+
322+
```python
323+
cursor.execute("SELECT COUNT(*) AS total FROM users WHERE active = true AND age > 30")
324+
```
325+
326+
**Aggregate with OR Conditions**
327+
328+
```python
329+
cursor.execute("SELECT COUNT(*) AS cnt FROM users WHERE age < 26 OR age > 40")
330+
```
331+
332+
**Note:** Aggregate functions are translated into a MongoDB `aggregate()` pipeline with `$match` (from WHERE), `$group` (with accumulators), and `$project` stages. `COUNT(*)` maps to `{$sum: 1}`, while `SUM`, `AVG`, `MIN`, and `MAX` map to their corresponding MongoDB accumulators (`$sum`, `$avg`, `$min`, `$max`).
333+
296334
### MongoDB Aggregate Function
297335

298336
PyMongoSQL supports executing native MongoDB aggregation pipelines using SQL-like syntax with the `aggregate()` function. This allows you to leverage MongoDB's powerful aggregation framework while maintaining SQL-style query patterns.
@@ -608,20 +646,24 @@ The table below shows how PyMongoSQL translates SQL operations into MongoDB comm
608646

609647
### SQL Operations to MongoDB Commands
610648

611-
| SQL Operation | MongoDB Command | Equivalent PyMongo Method |
612-
|---|---|---|
613-
| `SELECT ... FROM col` | `{find: col, projection: {...}}` | `db.command("find", ...)` |
614-
| `SELECT ... FROM col WHERE ...` | `{find: col, filter: {...}}` | `db.command("find", ...)` |
615-
| `SELECT ... ORDER BY col ASC/DESC` | `{find: ..., sort: {col: 1/-1}}` | `db.command("find", ...)` |
616-
| `SELECT ... LIMIT n` | `{find: ..., limit: n}` | `db.command("find", ...)` |
617-
| `SELECT ... OFFSET n` | `{find: ..., skip: n}` | `db.command("find", ...)` |
618-
| `SELECT * FROM col.aggregate(...)` | `collection.aggregate(pipeline)` | `collection.aggregate()` |
619-
| `INSERT INTO col ...` | `{insert: col, documents: [...]}` | `db.command("insert", ...)` |
620-
| `UPDATE col SET ... WHERE ...` | `{update: col, updates: [{q: filter, u: {$set: {...}}, multi: true}]}` | `db.command("update", ...)` |
621-
| `DELETE FROM col WHERE ...` | `{delete: col, deletes: [{q: filter, limit: 0}]}` | `db.command("delete", ...)` |
622-
| `CREATE VIEW v ON col AS '[...]'` | `{create: v, viewOn: col, pipeline: [...]}` | `db.command("create", ...)` |
623-
| `DROP VIEW v` | `{drop: v}` | `db.command("drop", ...)` |
624-
| `EXPLAIN <select>` | `{explain: <find\|aggregate cmd>, verbosity: "queryPlanner"}` | `db.command("explain", ...)` |
649+
| SQL Operation | MongoDB Command |
650+
|---|---|
651+
| `SELECT ... FROM col` | `{find: col, projection: {...}}` |
652+
| `SELECT ... FROM col WHERE ...` | `{find: col, filter: {...}}` |
653+
| `SELECT ... ORDER BY col ASC/DESC` | `{find: ..., sort: {col: 1/-1}}` |
654+
| `SELECT ... LIMIT n` | `{find: ..., limit: n}` |
655+
| `SELECT ... OFFSET n` | `{find: ..., skip: n}` |
656+
| `SELECT COUNT(*) FROM col` | `collection.aggregate([{$group: {_id: null, ...}}, {$project: ...}])` |
657+
| `SELECT AVG(field) FROM col` | `collection.aggregate([{$group: {_id: null, ...}}, {$project: ...}])` |
658+
| `SELECT MIN/MAX(field) FROM col` | `collection.aggregate([{$group: {_id: null, ...}}, {$project: ...}])` |
659+
| `SELECT SUM(field) FROM col` | `collection.aggregate([{$group: {_id: null, ...}}, {$project: ...}])` |
660+
| `SELECT * FROM col.aggregate(...)` | `collection.aggregate(pipeline)` |
661+
| `INSERT INTO col ...` | `{insert: col, documents: [...]}` |
662+
| `UPDATE col SET ... WHERE ...` | `{update: col, updates: [{q: filter, u: {$set: {...}}, multi: true}]}` |
663+
| `DELETE FROM col WHERE ...` | `{delete: col, deletes: [{q: filter, limit: 0}]}` |
664+
| `CREATE VIEW v ON col AS '[...]'` | `{create: v, viewOn: col, pipeline: [...]}` |
665+
| `DROP VIEW v` | `{drop: v}` |
666+
| `EXPLAIN <select>` | `{explain: <find\|aggregate cmd>, verbosity: "queryPlanner"}` |
625667

626668
### SQL Clauses to MongoDB Query Components
627669

@@ -635,6 +677,11 @@ The table below shows how PyMongoSQL translates SQL operations into MongoDB comm
635677
| `ORDER BY col DESC` | `sort: {col: -1}` | Descending sort |
636678
| `LIMIT n` | `limit: n` | Restrict result count |
637679
| `OFFSET n` | `skip: n` | Skip first n results |
680+
| `COUNT(*)` | `{$group: {_id: null, count: {$sum: 1}}}` | Document count |
681+
| `SUM(field)` | `{$group: {_id: null, sum: {$sum: "$field"}}}` | Field sum |
682+
| `AVG(field)` | `{$group: {_id: null, avg: {$avg: "$field"}}}` | Field average |
683+
| `MIN(field)` | `{$group: {_id: null, min: {$min: "$field"}}}` | Field minimum |
684+
| `MAX(field)` | `{$group: {_id: null, max: {$max: "$field"}}}` | Field maximum |
638685

639686
### WHERE Operators to MongoDB Filter Operators
640687

0 commit comments

Comments
 (0)