You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+57Lines changed: 57 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -26,6 +26,7 @@ PyMongoSQL implements the DB API 2.0 interfaces to provide SQL-like access to Mo
26
26
-**DB API 2.0 Compliant**: Full compatibility with Python Database API 2.0 specification
27
27
-**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
28
28
-**Nested Structure Support**: Query and filter deeply nested fields and arrays within MongoDB documents using standard SQL syntax
29
+
-**MongoDB Aggregate Pipeline Support**: Execute native MongoDB aggregation pipelines using SQL-like syntax with `aggregate()` function
29
30
-**SQLAlchemy Integration**: Complete ORM and Core support with dedicated MongoDB dialect
30
31
-**SQL Query Support**: SELECT statements with WHERE conditions, field selection, and aliases
31
32
-**DML Support**: Full support for INSERT, UPDATE, and DELETE operations using PartiQL syntax
@@ -235,6 +237,61 @@ Parameters are substituted into the MongoDB filter during execution, providing p
235
237
-**LIMIT**: `LIMIT 10`
236
238
-**Combined**: `ORDER BY created_at DESC LIMIT 5`
237
239
240
+
### MongoDB Aggregate Function
241
+
242
+
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.
243
+
244
+
**Syntax**
245
+
246
+
The `aggregate()` function accepts two parameters:
247
+
-**pipeline**: JSON string representing the MongoDB aggregation pipeline
248
+
-**options**: JSON string for aggregation options (optional, use '{}' for defaults)
"SELECT * FROM aggregate('[{\"$match\": {\"status\": \"active\"}}]', '{\"allowDiskUse\": true}')"
264
+
)
265
+
results = cursor.fetchall()
266
+
```
267
+
268
+
**Post-Aggregation Filtering and Sorting**
269
+
270
+
You can apply WHERE, ORDER BY, and LIMIT clauses after aggregation:
271
+
272
+
```python
273
+
# Filter aggregation results
274
+
cursor.execute(
275
+
"SELECT * FROM users.aggregate('[{\"$group\": {\"_id\": \"$city\", \"total\": {\"$sum\": 1}}}]', '{}') WHERE total > 100"
276
+
)
277
+
278
+
# Sort and limit aggregation results
279
+
cursor.execute(
280
+
"SELECT * FROM products.aggregate('[{\"$match\": {\"category\": \"Electronics\"}}]', '{}') ORDER BY price DESC LIMIT 10"
281
+
)
282
+
```
283
+
284
+
**Projection Support**
285
+
286
+
```python
287
+
# Select specific fields from aggregation results
288
+
cursor.execute(
289
+
"SELECT _id, total FROM users.aggregate('[{\"$group\": {\"_id\": \"$city\", \"total\": {\"$sum\": 1}}}]', '{}')"
290
+
)
291
+
```
292
+
293
+
**Note**: The pipeline and options must be valid JSON strings enclosed in single quotes. Post-aggregation filtering (WHERE), sorting (ORDER BY), and limiting (LIMIT) are applied in Python after the aggregation executes on MongoDB.
294
+
238
295
### INSERT Statements
239
296
240
297
PyMongoSQL supports inserting documents into MongoDB collections using both PartiQL-style object literals and standard SQL INSERT VALUES syntax.
0 commit comments