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
* Refactor the codebase to support DML statements
* Added basic insert support
* Restrict the superset mode
* Added delete support
* Added basic update support
* Update README to reflect new changes
* Refine README
---------
Co-authored-by: Peng Ren
@@ -16,10 +16,10 @@ PyMongoSQL is a Python [DB API 2.0 (PEP 249)](https://www.python.org/dev/peps/pe
16
16
17
17
PyMongoSQL implements the DB API 2.0 interfaces to provide SQL-like access to MongoDB, built on PartiQL syntax for querying semi-structured data. The project aims to:
18
18
19
-
- Bridge the gap between SQL and NoSQL by providing SQL capabilities for MongoDB's nested document structures
20
-
-Support standard SQL DQL (Data Query Language) operations including SELECT statements with WHERE, ORDER BY, and LIMIT clauses on nested and hierarchical data
21
-
-Provide seamless integration with existing Python applications that expect DB API 2.0 compliance
22
-
-Enable easy migration from traditional SQL databases to MongoDB without rewriting queries for document traversal
19
+
-**Bridge SQL and NoSQL**: Provide SQL capabilities for MongoDB's nested document structures
20
+
-**Standard SQL Operations**: Support DQL (SELECT) and DML (INSERT, UPDATE, DELETE) operations with WHERE, ORDER BY, and LIMIT clauses
21
+
-**Seamless Integration**: Full compatibility with Python applications expecting DB API 2.0 compliance
22
+
-**Easy Migration**: Enable migration from traditional SQL databases to MongoDB without rewriting application code
23
23
24
24
## Features
25
25
@@ -28,6 +28,7 @@ PyMongoSQL implements the DB API 2.0 interfaces to provide SQL-like access to Mo
28
28
-**Nested Structure Support**: Query and filter deeply nested fields and arrays within MongoDB documents using standard SQL syntax
29
29
-**SQLAlchemy Integration**: Complete ORM and Core support with dedicated MongoDB dialect
30
30
-**SQL Query Support**: SELECT statements with WHERE conditions, field selection, and aliases
31
+
-**DML Support**: Full support for INSERT, UPDATE, and DELETE operations using PartiQL syntax
31
32
-**Connection String Support**: MongoDB URI format for easy configuration
32
33
33
34
## Requirements
@@ -184,16 +185,18 @@ Parameters are substituted into the MongoDB filter during execution, providing p
184
185
## Supported SQL Features
185
186
186
187
### SELECT Statements
187
-
- Field selection: `SELECT name, age FROM users`
188
-
- Wildcards: `SELECT * FROM products`
189
-
-**Field aliases**: `SELECT name as user_name, age as user_age FROM users`
188
+
189
+
-**Field selection**: `SELECT name, age FROM users`
190
+
-**Wildcards**: `SELECT * FROM products`
191
+
-**Field aliases**: `SELECT name AS user_name, age AS user_age FROM users`
190
192
-**Nested fields**: `SELECT profile.name, profile.age FROM users`
191
193
-**Array access**: `SELECT items[0], items[1].name FROM orders`
192
194
193
195
### WHERE Clauses
194
-
- Equality: `WHERE name = 'John'`
195
-
- Comparisons: `WHERE age > 25`, `WHERE price <= 100.0`
196
-
- Logical operators: `WHERE age > 18 AND status = 'active'`
196
+
197
+
-**Equality**: `WHERE name = 'John'`
198
+
-**Comparisons**: `WHERE age > 25`, `WHERE price <= 100.0`
199
+
-**Logical operators**: `WHERE age > 18 AND status = 'active'`, `WHERE age < 30 OR role = 'admin'`
197
200
-**Nested field filtering**: `WHERE profile.status = 'active'`
@@ -206,9 +209,140 @@ Parameters are substituted into the MongoDB filter during execution, providing p
206
209
> **Note**: Avoid SQL reserved words (`user`, `data`, `value`, `count`, etc.) as unquoted field names. Use alternatives or bracket notation for arrays.
207
210
208
211
### Sorting and Limiting
209
-
- ORDER BY: `ORDER BY name ASC, age DESC`
210
-
- LIMIT: `LIMIT 10`
211
-
- Combined: `ORDER BY created_at DESC LIMIT 5`
212
+
213
+
-**ORDER BY**: `ORDER BY name ASC, age DESC`
214
+
-**LIMIT**: `LIMIT 10`
215
+
-**Combined**: `ORDER BY created_at DESC LIMIT 5`
216
+
217
+
### INSERT Statements
218
+
219
+
PyMongoSQL supports inserting documents into MongoDB collections using PartiQL-style object and bag literals.
220
+
221
+
**Single Document**
222
+
223
+
```python
224
+
cursor.execute(
225
+
"INSERT INTO Music {'title': 'Song A', 'artist': 'Alice', 'year': 2021}"
226
+
)
227
+
```
228
+
229
+
**Multiple Documents (Bag Syntax)**
230
+
231
+
```python
232
+
cursor.execute(
233
+
"INSERT INTO Music << {'title': 'Song B', 'artist': 'Bob'}, {'title': 'Song C', 'artist': 'Charlie'} >>"
234
+
)
235
+
```
236
+
237
+
**Parameterized INSERT**
238
+
239
+
```python
240
+
# Positional parameters using ? placeholders
241
+
cursor.execute(
242
+
"INSERT INTO Music {'title': ?, 'artist': ?, 'year': ?}",
243
+
["Song D", "Diana", 2020]
244
+
)
245
+
```
246
+
247
+
> **Note**: For parameterized INSERT, use positional parameters (`?`). Named placeholders (`:name`) are supported for SELECT, UPDATE, and DELETE queries.
248
+
249
+
### UPDATE Statements
250
+
251
+
PyMongoSQL supports updating documents in MongoDB collections using standard SQL UPDATE syntax.
252
+
253
+
**Update All Documents**
254
+
255
+
```python
256
+
cursor.execute("UPDATE Music SET available = false")
257
+
```
258
+
259
+
**Update with WHERE Clause**
260
+
261
+
```python
262
+
cursor.execute("UPDATE Music SET price = 14.99 WHERE year < 2020")
263
+
```
264
+
265
+
**Update Multiple Fields**
266
+
267
+
```python
268
+
cursor.execute(
269
+
"UPDATE Music SET price = 19.99, available = true WHERE artist = 'Alice'"
270
+
)
271
+
```
272
+
273
+
**Update with Logical Operators**
274
+
275
+
```python
276
+
cursor.execute(
277
+
"UPDATE Music SET price = 9.99 WHERE year = 2020 AND stock > 5"
278
+
)
279
+
```
280
+
281
+
**Parameterized UPDATE**
282
+
283
+
```python
284
+
# Positional parameters using ? placeholders
285
+
cursor.execute(
286
+
"UPDATE Music SET price = ?, stock = ? WHERE artist = ?",
287
+
[24.99, 50, "Bob"]
288
+
)
289
+
```
290
+
291
+
**Update Nested Fields**
292
+
293
+
```python
294
+
cursor.execute(
295
+
"UPDATE Music SET details.publisher = 'XYZ Records' WHERE title = 'Song A'"
296
+
)
297
+
```
298
+
299
+
**Check Updated Row Count**
300
+
301
+
```python
302
+
cursor.execute("UPDATE Music SET available = false WHERE year = 2020")
303
+
print(f"Updated {cursor.rowcount} documents")
304
+
```
305
+
306
+
### DELETE Statements
307
+
308
+
PyMongoSQL supports deleting documents from MongoDB collections using standard SQL DELETE syntax.
309
+
310
+
**Delete All Documents**
311
+
312
+
```python
313
+
cursor.execute("DELETE FROM Music")
314
+
```
315
+
316
+
**Delete with WHERE Clause**
317
+
318
+
```python
319
+
cursor.execute("DELETE FROM Music WHERE year < 2020")
320
+
```
321
+
322
+
**Delete with Logical Operators**
323
+
324
+
```python
325
+
cursor.execute(
326
+
"DELETE FROM Music WHERE year = 2019 AND available = false"
327
+
)
328
+
```
329
+
330
+
**Parameterized DELETE**
331
+
332
+
```python
333
+
# Positional parameters using ? placeholders
334
+
cursor.execute(
335
+
"DELETE FROM Music WHERE artist = ? AND year < ?",
336
+
["Charlie", 2021]
337
+
)
338
+
```
339
+
340
+
**Check Deleted Row Count**
341
+
342
+
```python
343
+
cursor.execute("DELETE FROM Music WHERE available = false")
344
+
print(f"Deleted {cursor.rowcount} documents")
345
+
```
212
346
213
347
## Apache Superset Integration
214
348
@@ -231,16 +365,18 @@ PyMongoSQL can be used as a database driver in Apache Superset for querying and
231
365
232
366
This allows seamless integration between MongoDB data and Superset's BI capabilities without requiring data migration to traditional SQL databases.
233
367
234
-
<h2style="color: red;">Limitations & Roadmap</h2>
368
+
## Limitations & Roadmap
235
369
236
-
**Note**: Currently PyMongoSQL focuses on Data Query Language (DQL) operations. The following SQL features are **not yet supported** but are planned for future releases:
370
+
**Note**: PyMongoSQL currently supports DQL (Data Query Language) and DML (Data Manipulation Language) operations. The following SQL features are **not yet supported** but are planned for future releases:
0 commit comments