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
+59-40Lines changed: 59 additions & 40 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -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
@@ -185,16 +185,18 @@ Parameters are substituted into the MongoDB filter during execution, providing p
185
185
## Supported SQL Features
186
186
187
187
### SELECT Statements
188
-
- Field selection: `SELECT name, age FROM users`
189
-
- Wildcards: `SELECT * FROM products`
190
-
-**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`
191
192
-**Nested fields**: `SELECT profile.name, profile.age FROM users`
192
193
-**Array access**: `SELECT items[0], items[1].name FROM orders`
193
194
194
195
### WHERE Clauses
195
-
- Equality: `WHERE name = 'John'`
196
-
- Comparisons: `WHERE age > 25`, `WHERE price <= 100.0`
197
-
- 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'`
198
200
-**Nested field filtering**: `WHERE profile.status = 'active'`
@@ -207,83 +209,94 @@ Parameters are substituted into the MongoDB filter during execution, providing p
207
209
> **Note**: Avoid SQL reserved words (`user`, `data`, `value`, `count`, etc.) as unquoted field names. Use alternatives or bracket notation for arrays.
208
210
209
211
### Sorting and Limiting
210
-
- ORDER BY: `ORDER BY name ASC, age DESC`
211
-
- LIMIT: `LIMIT 10`
212
-
- 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`
213
216
214
217
### INSERT Statements
215
218
216
219
PyMongoSQL supports inserting documents into MongoDB collections using PartiQL-style object and bag literals.
217
220
218
-
-**Single Document**
221
+
**Single Document**
219
222
220
223
```python
221
-
cursor.execute("INSERT INTO Music {'title': 'Song A', 'artist': 'Alice', 'year': 2021}")
224
+
cursor.execute(
225
+
"INSERT INTO Music {'title': 'Song A', 'artist': 'Alice', 'year': 2021}"
226
+
)
222
227
```
223
228
224
-
-**Multiple Documents (Bag Syntax)**
229
+
**Multiple Documents (Bag Syntax)**
225
230
226
231
```python
227
232
cursor.execute(
228
-
"INSERT INTO Music << {'title': 'Song B', 'artist': 'Bob'}, {'title': 'Song C', 'artist': 'Charlie'} >>"
233
+
"INSERT INTO Music << {'title': 'Song B', 'artist': 'Bob'}, {'title': 'Song C', 'artist': 'Charlie'} >>"
229
234
)
230
235
```
231
236
232
-
-**Parameterized INSERT (qmark placeholders)**
237
+
**Parameterized INSERT**
233
238
234
239
```python
235
240
# Positional parameters using ? placeholders
236
241
cursor.execute(
237
-
"INSERT INTO Music {'title': ?, 'artist': ?, 'year': ?}",
238
-
["Song D", "Diana", 2020]
242
+
"INSERT INTO Music {'title': ?, 'artist': ?, 'year': ?}",
243
+
["Song D", "Diana", 2020]
239
244
)
240
245
```
241
246
242
-
> Note: For INSERT, use positional parameters (`?`). Named placeholders (`:name`) are supported for SELECT queries; INSERT currently recommends `?` style.
247
+
> **Note**: For parameterized INSERT, use positional parameters (`?`). Named placeholders (`:name`) are supported for SELECT, UPDATE, and DELETE queries.
243
248
244
249
### UPDATE Statements
245
250
246
251
PyMongoSQL supports updating documents in MongoDB collections using standard SQL UPDATE syntax.
247
252
248
-
-**Update All Documents**
253
+
**Update All Documents**
249
254
250
255
```python
251
256
cursor.execute("UPDATE Music SET available = false")
252
257
```
253
258
254
-
-**Update Multiple Fields**
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**
255
266
256
267
```python
257
268
cursor.execute(
258
-
"UPDATE Music SET price = 19.99, available = true WHERE artist = 'Alice'"
269
+
"UPDATE Music SET price = 19.99, available = true WHERE artist = 'Alice'"
259
270
)
260
271
```
261
272
262
-
-**Update with Comparisons and Logical Operators**
273
+
**Update with Logical Operators**
263
274
264
275
```python
265
276
cursor.execute(
266
-
"UPDATE Music SET price = 9.99 WHERE year = 2020 AND stock > 5"
277
+
"UPDATE Music SET price = 9.99 WHERE year = 2020 AND stock > 5"
267
278
)
268
279
```
269
280
270
-
-**Parameterized UPDATE**
281
+
**Parameterized UPDATE**
271
282
272
283
```python
273
284
# Positional parameters using ? placeholders
274
285
cursor.execute(
275
-
"UPDATE Music SET price = ?, stock = ? WHERE artist = ?",
276
-
[24.99, 50, "Bob"]
286
+
"UPDATE Music SET price = ?, stock = ? WHERE artist = ?",
287
+
[24.99, 50, "Bob"]
277
288
)
278
289
```
279
290
280
-
-**Update Nested Fields**
291
+
**Update Nested Fields**
281
292
282
293
```python
283
-
cursor.execute("UPDATE Music SET details.publisher = 'XYZ Records' WHERE title = 'Song A'")
294
+
cursor.execute(
295
+
"UPDATE Music SET details.publisher = 'XYZ Records' WHERE title = 'Song A'"
296
+
)
284
297
```
285
298
286
-
-**Check Updated Row Count**
299
+
**Check Updated Row Count**
287
300
288
301
```python
289
302
cursor.execute("UPDATE Music SET available = false WHERE year = 2020")
PyMongoSQL supports deleting documents from MongoDB collections using standard SQL DELETE syntax.
296
309
297
-
-**Delete All Documents**
310
+
**Delete All Documents**
298
311
299
312
```python
300
313
cursor.execute("DELETE FROM Music")
301
314
```
302
315
303
-
-**Delete with Logical Operators**
316
+
**Delete with WHERE Clause**
317
+
318
+
```python
319
+
cursor.execute("DELETE FROM Music WHERE year < 2020")
320
+
```
321
+
322
+
**Delete with Logical Operators**
304
323
305
324
```python
306
325
cursor.execute(
307
-
"DELETE FROM Music WHERE year = 2019 AND available = false"
326
+
"DELETE FROM Music WHERE year = 2019 AND available = false"
308
327
)
309
328
```
310
329
311
-
-**Parameterized DELETE**
330
+
**Parameterized DELETE**
312
331
313
332
```python
314
333
# Positional parameters using ? placeholders
315
334
cursor.execute(
316
-
"DELETE FROM Music WHERE artist = ? AND year < ?",
317
-
["Charlie", 2021]
335
+
"DELETE FROM Music WHERE artist = ? AND year < ?",
336
+
["Charlie", 2021]
318
337
)
319
338
```
320
339
321
-
-**Check Deleted Row Count**
340
+
**Check Deleted Row Count**
322
341
323
342
```python
324
343
cursor.execute("DELETE FROM Music WHERE available = false")
@@ -346,7 +365,7 @@ PyMongoSQL can be used as a database driver in Apache Superset for querying and
346
365
347
366
This allows seamless integration between MongoDB data and Superset's BI capabilities without requiring data migration to traditional SQL databases.
348
367
349
-
<h2style="color: red;">Limitations & Roadmap</h2>
368
+
## Limitations & Roadmap
350
369
351
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