Skip to content

Commit aa1bb1e

Browse files
author
Peng Ren
committed
Refine README
1 parent 5e9f5b2 commit aa1bb1e

File tree

1 file changed

+59
-40
lines changed

1 file changed

+59
-40
lines changed

README.md

Lines changed: 59 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ PyMongoSQL is a Python [DB API 2.0 (PEP 249)](https://www.python.org/dev/peps/pe
1616

1717
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:
1818

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
2323

2424
## Features
2525

@@ -185,16 +185,18 @@ Parameters are substituted into the MongoDB filter during execution, providing p
185185
## Supported SQL Features
186186

187187
### 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`
191192
- **Nested fields**: `SELECT profile.name, profile.age FROM users`
192193
- **Array access**: `SELECT items[0], items[1].name FROM orders`
193194

194195
### 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'`
198200
- **Nested field filtering**: `WHERE profile.status = 'active'`
199201
- **Array filtering**: `WHERE items[0].price > 100`
200202

@@ -207,83 +209,94 @@ Parameters are substituted into the MongoDB filter during execution, providing p
207209
> **Note**: Avoid SQL reserved words (`user`, `data`, `value`, `count`, etc.) as unquoted field names. Use alternatives or bracket notation for arrays.
208210
209211
### 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`
213216

214217
### INSERT Statements
215218

216219
PyMongoSQL supports inserting documents into MongoDB collections using PartiQL-style object and bag literals.
217220

218-
- **Single Document**
221+
**Single Document**
219222

220223
```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+
)
222227
```
223228

224-
- **Multiple Documents (Bag Syntax)**
229+
**Multiple Documents (Bag Syntax)**
225230

226231
```python
227232
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'} >>"
229234
)
230235
```
231236

232-
- **Parameterized INSERT (qmark placeholders)**
237+
**Parameterized INSERT**
233238

234239
```python
235240
# Positional parameters using ? placeholders
236241
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]
239244
)
240245
```
241246

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.
243248
244249
### UPDATE Statements
245250

246251
PyMongoSQL supports updating documents in MongoDB collections using standard SQL UPDATE syntax.
247252

248-
- **Update All Documents**
253+
**Update All Documents**
249254

250255
```python
251256
cursor.execute("UPDATE Music SET available = false")
252257
```
253258

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**
255266

256267
```python
257268
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'"
259270
)
260271
```
261272

262-
- **Update with Comparisons and Logical Operators**
273+
**Update with Logical Operators**
263274

264275
```python
265276
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"
267278
)
268279
```
269280

270-
- **Parameterized UPDATE**
281+
**Parameterized UPDATE**
271282

272283
```python
273284
# Positional parameters using ? placeholders
274285
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"]
277288
)
278289
```
279290

280-
- **Update Nested Fields**
291+
**Update Nested Fields**
281292

282293
```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+
)
284297
```
285298

286-
- **Check Updated Row Count**
299+
**Check Updated Row Count**
287300

288301
```python
289302
cursor.execute("UPDATE Music SET available = false WHERE year = 2020")
@@ -294,31 +307,37 @@ print(f"Updated {cursor.rowcount} documents")
294307

295308
PyMongoSQL supports deleting documents from MongoDB collections using standard SQL DELETE syntax.
296309

297-
- **Delete All Documents**
310+
**Delete All Documents**
298311

299312
```python
300313
cursor.execute("DELETE FROM Music")
301314
```
302315

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**
304323

305324
```python
306325
cursor.execute(
307-
"DELETE FROM Music WHERE year = 2019 AND available = false"
326+
"DELETE FROM Music WHERE year = 2019 AND available = false"
308327
)
309328
```
310329

311-
- **Parameterized DELETE**
330+
**Parameterized DELETE**
312331

313332
```python
314333
# Positional parameters using ? placeholders
315334
cursor.execute(
316-
"DELETE FROM Music WHERE artist = ? AND year < ?",
317-
["Charlie", 2021]
335+
"DELETE FROM Music WHERE artist = ? AND year < ?",
336+
["Charlie", 2021]
318337
)
319338
```
320339

321-
- **Check Deleted Row Count**
340+
**Check Deleted Row Count**
322341

323342
```python
324343
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
346365

347366
This allows seamless integration between MongoDB data and Superset's BI capabilities without requiring data migration to traditional SQL databases.
348367

349-
<h2 style="color: red;">Limitations & Roadmap</h2>
368+
## Limitations & Roadmap
350369

351370
**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:
352371

0 commit comments

Comments
 (0)