### Install PyMongoSQL from Source Source: https://pypi.org/project/pymongosql/0.7.0 Clone the repository and install PyMongoSQL in editable mode. ```bash git clone https://github.com/passren/PyMongoSQL.git cd PyMongoSQL pip install -e . ``` -------------------------------- ### Install PyMongoSQL Source: https://pypi.org/project/pymongosql Install PyMongoSQL using pip. For source installation, clone the repository and use pip install -e. ```bash pip install pymongosql ``` ```bash git clone https://github.com/passren/PyMongoSQL.git cd PyMongoSQL pip install -e . ``` -------------------------------- ### Install PyMongoSQL Source: https://pypi.org/project/pymongosql Install PyMongoSQL using pip. This command should be run on the Superset app server. ```bash pip install pymongosql ``` -------------------------------- ### Nested Field and Array Access Examples Source: https://pypi.org/project/pymongosql/0.7.0 Demonstrates accessing single-level, multi-level, and array elements within nested document structures for querying. ```SQL profile.name ``` ```SQL settings.theme ``` ```SQL account.profile.name ``` ```SQL config.database.host ``` ```SQL items[0].name ``` ```SQL orders[1].total ``` ```SQL WHERE customer.profile.age > 18 AND orders[0].status = 'paid' ``` -------------------------------- ### Aggregate with OR Conditions Source: https://pypi.org/project/pymongosql Use aggregate functions with OR conditions in the WHERE clause to count records that meet either of the specified criteria. This example counts users based on age ranges. ```python cursor.execute("SELECT COUNT(*) AS cnt FROM users WHERE age < 26 OR age > 40") ``` -------------------------------- ### Aggregate with WHERE Clause Source: https://pypi.org/project/pymongosql Combine aggregate functions with a WHERE clause to filter records before aggregation. This example counts users based on active status and age. ```python cursor.execute("SELECT COUNT(*) AS total FROM users WHERE active = true AND age > 30") ``` -------------------------------- ### Count Total Records with COUNT(*) Source: https://pypi.org/project/pymongosql Use COUNT(*) to get the total number of records in a collection. The result is aliased as 'total'. ```python cursor.execute("SELECT COUNT(*) AS total FROM users") row = cursor.fetchone() print(f"Total users: {row[0]}") ``` -------------------------------- ### Basic SELECT Statements Source: https://pypi.org/project/pymongosql/0.7.0 Demonstrates basic field selection, wildcards, aliases, nested fields, and array access in SELECT statements. ```SQL SELECT name, age FROM users ``` ```SQL SELECT * FROM products ``` ```SQL SELECT name AS user_name, age AS user_age FROM users ``` ```SQL SELECT profile.name, profile.age FROM users ``` ```SQL SELECT items[0], items[1].name FROM orders ``` -------------------------------- ### Explain MongoDB Query Plans Source: https://pypi.org/project/pymongosql Prefix `SELECT` statements with `EXPLAIN` to inspect MongoDB query plans. Supported verbosities include `queryPlanner`, `executionStats`, and `allPlansExecution`. The output is flattened into `stage` and `details` columns. ```python # Default verbosity: queryPlanner cursor.execute("EXPLAIN SELECT * FROM users WHERE age > 21") for stage, details in cursor.fetchall(): print(stage, details) # Request executionStats (actual timing, docs/keys examined) or allPlansExecution cursor.execute("EXPLAIN (verbosity executionStats) SELECT * FROM users WHERE age > 21") ``` -------------------------------- ### Basic PyMongoSQL Connection and Query Source: https://pypi.org/project/pymongosql Connect to a MongoDB instance and execute a simple SELECT query using the default cursor. ```python from pymongosql import connect # Connect to MongoDB connection = connect( host="mongodb://localhost:27017", database="database" ) cursor = connection.cursor() cursor.execute('SELECT name, email FROM users WHERE age > 25') print(cursor.fetchall()) ``` -------------------------------- ### CREATE VIEW with Lookup Pipeline Source: https://pypi.org/project/pymongosql/0.7.0 Create a MongoDB view with a $lookup (join) pipeline using SQL syntax. The pipeline must be a valid JSON array string enclosed in single quotes. ```python import json # Create a view with a $lookup (join) pipeline pipeline = json.dumps([ {"\$lookup": {"from": "orders", "localField": "_id", "foreignField": "user_id", "as": "user_orders"}} ]) cursor.execute(f"CREATE VIEW users_with_orders ON users AS '{pipeline}'") # Query the view like any collection cursor.execute("SELECT * FROM active_users") ``` -------------------------------- ### PyMongoSQL Cursor vs DictCursor Access Source: https://pypi.org/project/pymongosql Demonstrates accessing data from default Cursor (tuple) and DictCursor (dictionary). ```python cursor = connection.cursor() cursor.execute('SELECT name, email FROM users') row = cursor.fetchone() print(row[0]) # Access by index ``` ```python from pymongosql.cursor import DictCursor cursor = connection.cursor(DictCursor) cursor.execute('SELECT name, email FROM users') row = cursor.fetchone() print(row['name']) # Access by column name ``` -------------------------------- ### Create MongoDB Views with SQL Source: https://pypi.org/project/pymongosql Use `CREATE VIEW` with a JSON pipeline to define MongoDB views. The pipeline must be a valid JSON array string enclosed in single quotes. This maps to `db.command({"create": view_name, "viewOn": collection, "pipeline": [...]})`. ```python import json # Create a view with a filter pipeline pipeline = json.dumps([{" $match": {"active": True}}]) cursor.execute(f"CREATE VIEW active_users ON users AS '{pipeline}'") # Create a view with a $lookup (join) pipeline pipeline = json.dumps([ {" $lookup": {"from": "orders", "localField": "_id", "foreignField": "user_id", "as": "user_orders"}} ]) cursor.execute(f"CREATE VIEW users_with_orders ON users AS '{pipeline}'") # Query the view like any collection cursor.execute("SELECT * FROM active_users") ``` -------------------------------- ### Connect to MongoDB with Superset Mode Source: https://pypi.org/project/pymongosql/0.7.0 Connect to your MongoDB instance using the connection URI with superset mode. This is used for integrating with Apache Superset. ```text mongodb://username:password@host:port/database?mode=superset ``` ```text mongodb+srv://username:password@host/database?mode=superset ``` -------------------------------- ### SQL Operations to MongoDB Commands Source: https://pypi.org/project/pymongosql/0.7.0 This table outlines the direct translation of common SQL operations into their corresponding MongoDB commands and PyMongo methods. ```SQL SELECT ... FROM col ``` ```MongoDB {find: col, projection: {...}} ``` ```SQL SELECT ... FROM col WHERE ... ``` ```MongoDB {find: col, filter: {...}} ``` ```SQL SELECT ... ORDER BY col ASC/DESC ``` ```MongoDB {find: ..., sort: {col: 1/-1}} ``` ```SQL SELECT ... LIMIT n ``` ```MongoDB {find: ..., limit: n} ``` ```SQL SELECT ... OFFSET n ``` ```MongoDB {find: ..., skip: n} ``` ```SQL SELECT * FROM col.aggregate(...) ``` ```MongoDB collection.aggregate(pipeline) ``` ```SQL INSERT INTO col ... ``` ```MongoDB {insert: col, documents: [...]} ``` ```SQL UPDATE col SET ... WHERE ... ``` ```MongoDB {update: col, updates: [{q: filter, u: {$set: {...}}, multi: true}]} ``` ```SQL DELETE FROM col WHERE ... ``` ```MongoDB {delete: col, deletes: [{q: filter, limit: 0}]} ``` ```SQL CREATE VIEW v ON col AS '[...]' ``` ```MongoDB {create: v, viewOn: col, pipeline: [...]} ``` ```SQL DROP VIEW v ``` ```MongoDB {drop: v} ``` ```SQL EXPLAIN