Skip to content

Commit 201649b

Browse files
committed
add MongoDB client metadata and Update README
1 parent c6995df commit 201649b

File tree

4 files changed

+80
-7
lines changed

4 files changed

+80
-7
lines changed

README.md

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ pip install pymongosql
6666
Or install from source:
6767

6868
```bash
69-
git clone https://github.com/your-username/PyMongoSQL.git
69+
git clone https://github.com/passren/PyMongoSQL.git
7070
cd PyMongoSQL
7171
pip install -e .
7272
```
@@ -90,6 +90,7 @@ pip install -e .
9090
- [UPDATE Statements](#update-statements)
9191
- [DELETE Statements](#delete-statements)
9292
- [Transaction Support](#transaction-support)
93+
- [SQL to MongoDB Mapping](#sql-to-mongodb-mapping)
9394
- [Apache Superset Integration](#apache-superset-integration)
9495
- [Limitations & Roadmap](#limitations--roadmap)
9596
- [Contributing](#contributing)
@@ -534,6 +535,76 @@ finally:
534535

535536
**Note:** MongoDB requires a replica set or sharded cluster for transaction support. Standalone MongoDB servers do not support ACID transactions at the server level.
536537

538+
## SQL to MongoDB Mapping
539+
540+
The table below shows how PyMongoSQL translates SQL operations into MongoDB commands.
541+
542+
### SQL Operations to MongoDB Commands
543+
544+
| SQL Operation | MongoDB Command | Equivalent PyMongo Method |
545+
|---|---|---|
546+
| `SELECT ... FROM col` | `{find: col, projection: {...}}` | `db.command("find", ...)` |
547+
| `SELECT ... FROM col WHERE ...` | `{find: col, filter: {...}}` | `db.command("find", ...)` |
548+
| `SELECT ... ORDER BY col ASC/DESC` | `{find: ..., sort: {col: 1/-1}}` | `db.command("find", ...)` |
549+
| `SELECT ... LIMIT n` | `{find: ..., limit: n}` | `db.command("find", ...)` |
550+
| `SELECT ... OFFSET n` | `{find: ..., skip: n}` | `db.command("find", ...)` |
551+
| `SELECT * FROM col.aggregate(...)` | `collection.aggregate(pipeline)` | `collection.aggregate()` |
552+
| `INSERT INTO col ...` | `{insert: col, documents: [...]}` | `db.command("insert", ...)` |
553+
| `UPDATE col SET ... WHERE ...` | `{update: col, updates: [{q: filter, u: {$set: {...}}, multi: true}]}` | `db.command("update", ...)` |
554+
| `DELETE FROM col WHERE ...` | `{delete: col, deletes: [{q: filter, limit: 0}]}` | `db.command("delete", ...)` |
555+
556+
### SQL Clauses to MongoDB Query Components
557+
558+
| SQL Clause | MongoDB Equivalent | Example |
559+
|---|---|---|
560+
| `SELECT col1, col2` | `projection: {col1: 1, col2: 1}` | Fields to include |
561+
| `SELECT *` | _(no projection)_ | Returns all fields |
562+
| `SELECT col AS alias` | Column alias applied in result set | Post-processing rename |
563+
| `FROM collection` | `find: "collection"` | Target collection |
564+
| `ORDER BY col ASC` | `sort: {col: 1}` | Ascending sort |
565+
| `ORDER BY col DESC` | `sort: {col: -1}` | Descending sort |
566+
| `LIMIT n` | `limit: n` | Restrict result count |
567+
| `OFFSET n` | `skip: n` | Skip first n results |
568+
569+
### WHERE Operators to MongoDB Filter Operators
570+
571+
| SQL WHERE Clause | MongoDB Filter | Notes |
572+
|---|---|---|
573+
| `field = value` | `{field: value}` | Equality shorthand |
574+
| `field != value` | `{field: {$ne: value}}` | Not equal |
575+
| `field > value` | `{field: {$gt: value}}` | Greater than |
576+
| `field >= value` | `{field: {$gte: value}}` | Greater than or equal |
577+
| `field < value` | `{field: {$lt: value}}` | Less than |
578+
| `field <= value` | `{field: {$lte: value}}` | Less than or equal |
579+
| `field LIKE 'pat%'` | `{field: {$regex: "pat.*"}}` | `%``.*`, `_``.` |
580+
| `field IN (a, b, c)` | `{field: {$in: [a, b, c]}}` | Match any value in list |
581+
| `field NOT IN (a, b)` | `{field: {$nin: [a, b]}}` | Exclude values in list |
582+
| `field BETWEEN a AND b` | `{$and: [{field: {$gte: a}}, {field: {$lte: b}}]}` | Range filter |
583+
| `field IS NULL` | `{field: {$eq: null}}` | Null check |
584+
| `field IS NOT NULL` | `{field: {$ne: null}}` | Not null check |
585+
| `cond1 AND cond2` | `{$and: [filter1, filter2]}` | Logical AND |
586+
| `cond1 OR cond2` | `{$or: [filter1, filter2]}` | Logical OR |
587+
| `NOT cond` | `{$not: filter}` | Logical NOT |
588+
589+
### Nested Field and Array Access
590+
591+
| SQL Syntax | MongoDB Dot Notation | Example |
592+
|---|---|---|
593+
| `profile.name` | `profile.name` | Single-level nesting |
594+
| `account.profile.name` | `account.profile.name` | Multi-level nesting |
595+
| `items[0].name` | `items.0.name` | Array index access |
596+
597+
### DML Mapping Details
598+
599+
| SQL DML | MongoDB Behavior | Notes |
600+
|---|---|---|
601+
| `INSERT INTO col VALUE {...}` | Single document insert | PartiQL object literal |
602+
| `INSERT INTO col VALUE << {...}, {...} >>` | Multi-document insert | PartiQL bag syntax |
603+
| `INSERT INTO col (c1, c2) VALUES (v1, v2)` | Columns and values zipped into document | Standard SQL syntax |
604+
| `UPDATE col SET f1 = v1` | `{$set: {f1: v1}}` with `multi: true` | Updates all matching docs |
605+
| `DELETE FROM col` | `{q: {}, limit: 0}` | Deletes all documents |
606+
| `DELETE FROM col WHERE ...` | `{q: filter, limit: 0}` | Deletes all matching docs |
607+
537608
## Apache Superset Integration
538609

539610
PyMongoSQL can be used as a database driver in Apache Superset for querying and visualizing MongoDB data:

pymongosql/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
if TYPE_CHECKING:
77
from .connection import Connection
88

9-
__version__: str = "0.4.7"
9+
__version__: str = "0.4.8"
1010

1111
# Globals https://www.python.org/dev/peps/pep-0249/#globals
1212
apilevel: str = "2.0"

pymongosql/connection.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,19 @@
1111
from pymongo.driver_info import DriverInfo
1212
from pymongo.errors import ConnectionFailure
1313

14+
from .common import BaseCursor
15+
from .cursor import Cursor
16+
from .error import DatabaseError, OperationalError
17+
from .helper import ConnectionHelper
18+
from .retry import RetryConfig, execute_with_retry
19+
1420
try:
1521
_VERSION = _get_version("pymongosql")
1622
except Exception:
1723
_VERSION = None
1824

1925
_DRIVER_INFO = DriverInfo(name="PyMongoSQL", version=_VERSION)
2026

21-
from .common import BaseCursor
22-
from .cursor import Cursor
23-
from .error import DatabaseError, OperationalError
24-
from .helper import ConnectionHelper
25-
from .retry import RetryConfig, execute_with_retry
2627

2728
_logger = logging.getLogger(__name__)
2829

tests/run_test_server.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
66
This script helps manage MongoDB instances for testing PyMongoSQL.
77
"""
8+
89
import json
910
import os
1011
import subprocess

0 commit comments

Comments
 (0)