Skip to content

Commit abcaa1d

Browse files
anandgupta42claude
andauthored
feat: add MongoDB driver support (#482)
* feat: add MongoDB driver support Add MongoDB as the 11th supported database driver, enabling document database operations via the existing `Connector` interface. - New driver at `packages/drivers/src/mongodb.ts` supporting: - Full MQL command set: find, aggregate, CRUD, indexes, collection mgmt - Cross-database queries via per-query `database` field - BSON type serialization (ObjectId, Decimal128, Long, UUID, Binary, Date) - Schema introspection via document sampling with field type inference - Connection string URI and host/port/user/password auth - `authorizedDatabases` fallback for restricted-privilege users - Registration in driver index, config normalizer (with `mongo`/`mongodb` aliases), and connection registry - CI: MongoDB 7.0 service in driver-e2e workflow with health checks - 90 E2E tests including: - CRUD operations, aggregation pipelines, schema introspection - Cross-database operations, index management, collection lifecycle - Adversarial tests: deeply nested docs, special characters, heterogeneous collections, concurrent operations, large documents, numeric edge cases Closes #480 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: address multi-model code review findings for MongoDB driver Fixes from 6-model consensus review (Claude, GPT 5.2 Codex, Gemini 3.1 Pro, Kimi K2.5, MiniMax M2.5, GLM-5): - Cap user-specified `limit` against `effectiveLimit` to prevent OOM from unbounded queries (flagged by Gemini) - Add `ping` command for connection testing instead of querying `system.version` which may not be accessible (flagged by Gemini, GLM-5) - Add `e.code === 26` fallback for `dropCollection` error handling across MongoDB versions (flagged by GLM-5) - Fix misleading docstring on `extractFields` that claimed dot-notation expansion (flagged by Gemini) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: address CodeRabbit findings and update docs for MongoDB driver CodeRabbit review fixes: - Use `...COMMON_ALIASES` spread in `MONGODB_ALIASES` for consistency with other drivers (normalize.ts) - Scan entire aggregate pipeline for `$limit` instead of only last stage; also skip auto-limit for `$out`/`$merge` write pipelines - Keep `mongodb` at `^6.0.0` (NOT upgrading to v7): v7 drops Node 16/18 support and has many breaking changes; v6.21.0 is actively maintained and supports MongoDB server 3.6-8.0 Additional review fixes: - Include database name in URI when building from individual config fields for correct auth-source resolution (GLM-5) - Add security comment about credential exposure in URI (Kimi K2.5) Documentation updates: - `docs/docs/drivers.md`: add MongoDB to support matrix (11 databases), install section, auth methods, auto-discovery - `docs/docs/configure/warehouses.md`: add MongoDB configuration section with connection string and field-based examples, server compatibility - `docs/docs/data-engineering/tools/warehouse-tools.md`: add MongoDB to Docker discovery and env var detection Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * style: format MongoDB driver files with Prettier Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: cap aggregate `$limit` and serialize `distinct` values (CodeRabbit) - Cap user-specified `$limit` in aggregate pipelines against `effectiveLimit` to prevent OOM, matching the `find` command behavior - Serialize `distinct` return values through `serializeValue()` for consistent BSON type handling across find/aggregate/distinct Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 91fe351 commit abcaa1d

File tree

11 files changed

+2923
-30
lines changed

11 files changed

+2923
-30
lines changed

.github/workflows/ci.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ jobs:
4545
- 'packages/opencode/src/altimate/native/connections/**'
4646
- 'packages/opencode/test/altimate/drivers-e2e.test.ts'
4747
- 'packages/opencode/test/altimate/drivers-docker-e2e.test.ts'
48+
- 'packages/opencode/test/altimate/drivers-mongodb-e2e.test.ts'
4849
- 'packages/opencode/test/altimate/connections.test.ts'
4950
dbt-tools:
5051
- 'packages/dbt-tools/**'
@@ -155,6 +156,16 @@ jobs:
155156
--health-timeout 5s
156157
--health-retries 10
157158
159+
mongodb:
160+
image: mongo:7.0
161+
ports:
162+
- 27017:27017
163+
options: >-
164+
--health-cmd "mongosh --eval 'db.runCommand({ping:1})' --quiet"
165+
--health-interval 5s
166+
--health-timeout 5s
167+
--health-retries 10
168+
158169
steps:
159170
- uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
160171

@@ -195,6 +206,13 @@ jobs:
195206
TEST_REDSHIFT_PORT: "15439"
196207
TEST_REDSHIFT_PASSWORD: testpass123
197208

209+
- name: Run MongoDB driver E2E
210+
run: bun test test/altimate/drivers-mongodb-e2e.test.ts
211+
working-directory: packages/opencode
212+
env:
213+
TEST_MONGODB_HOST: 127.0.0.1
214+
TEST_MONGODB_PORT: "27017"
215+
198216
# Cloud tests NOT included — they require real credentials
199217
# Run locally with:
200218
# ALTIMATE_CODE_CONN_SNOWFLAKE_TEST='...' bun test test/altimate/drivers-snowflake-e2e.test.ts

bun.lock

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/docs/configure/warehouses.md

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Warehouses
22

3-
Altimate Code connects to 8 warehouse types. Configure them in `.altimate-code/connections.json` (project-local) or `~/.altimate-code/connections.json` (global).
3+
Altimate Code connects to 9 warehouse types. Configure them in `.altimate-code/connections.json` (project-local) or `~/.altimate-code/connections.json` (global).
44

55
## Configuration
66

@@ -237,6 +237,51 @@ If you're already authenticated via `gcloud`, omit `credentials_path`:
237237
| `ssl_cert` | No | Path to client certificate file |
238238
| `ssl_key` | No | Path to client key file |
239239

240+
## MongoDB
241+
242+
```json
243+
{
244+
"my-mongodb": {
245+
"type": "mongodb",
246+
"host": "localhost",
247+
"port": 27017,
248+
"database": "analytics",
249+
"user": "analyst",
250+
"password": "{env:MONGO_PASSWORD}"
251+
}
252+
}
253+
```
254+
255+
| Field | Required | Description |
256+
|-------|----------|-------------|
257+
| `connection_string` | No | Full connection string (alternative to individual fields) |
258+
| `host` | No | Hostname (default: `127.0.0.1`) |
259+
| `port` | No | Port (default: `27017`) |
260+
| `database` | No | Database name |
261+
| `user` | No | Username |
262+
| `password` | No | Password |
263+
| `auth_source` | No | Authentication database (default: `admin`) |
264+
| `replica_set` | No | Replica set name |
265+
| `tls` | No | Enable TLS (default: `false`) |
266+
| `direct_connection` | No | Connect directly to a single host |
267+
268+
### Using a connection string
269+
270+
```json
271+
{
272+
"my-mongodb": {
273+
"type": "mongodb",
274+
"connection_string": "mongodb://analyst:secret@localhost:27017/analytics"
275+
}
276+
}
277+
```
278+
279+
!!! note
280+
MongoDB uses MQL (MongoDB Query Language) instead of SQL. Queries are submitted as JSON objects via the `execute` method. Supported commands: `find`, `aggregate`, `countDocuments`, `distinct`, `insertOne`, `insertMany`, `updateOne`, `updateMany`, `deleteOne`, `deleteMany`, `createIndex`, `listIndexes`, `createCollection`, `dropCollection`, `ping`.
281+
282+
!!! info "Server compatibility"
283+
The MongoDB driver (v6.x) supports MongoDB server versions 3.6 through 8.0, covering all releases from the last 3+ years.
284+
240285
## SQL Server
241286

242287
```json

docs/docs/data-engineering/tools/warehouse-tools.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ env_bigquery | bigquery | GOOGLE_APPLICATION_CREDENTIALS
7070
| Databricks | `DATABRICKS_HOST`, `DATABRICKS_SERVER_HOSTNAME` |
7171
| PostgreSQL | `PGHOST`, `PGDATABASE`, `DATABASE_URL` |
7272
| MySQL | `MYSQL_HOST`, `MYSQL_DATABASE` |
73+
| MongoDB | `MONGODB_URI`, `MONGO_URL` |
7374
| Redshift | `REDSHIFT_HOST` |
7475

7576
### Parameters
@@ -164,7 +165,7 @@ Remove an existing warehouse connection.
164165

165166
## warehouse_discover
166167

167-
Discover database containers running in Docker. Detects PostgreSQL, MySQL/MariaDB, and SQL Server containers with their connection details.
168+
Discover database containers running in Docker. Detects PostgreSQL, MySQL/MariaDB, SQL Server, and MongoDB containers with their connection details.
168169

169170
```
170171
> warehouse_discover

docs/docs/drivers.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Overview
44

5-
Altimate Code connects to 10 databases natively via TypeScript drivers. No Python dependency required. Drivers are loaded lazily, so only the driver you need is imported at runtime.
5+
Altimate Code connects to 11 databases natively via TypeScript drivers. No Python dependency required. Drivers are loaded lazily, so only the driver you need is imported at runtime.
66

77
## Support Matrix
88

@@ -17,6 +17,7 @@ Altimate Code connects to 10 databases natively via TypeScript drivers. No Pytho
1717
| Snowflake | `snowflake-sdk` | Password, Key-Pair (unencrypted + encrypted), OAuth | ✅ Live account | 37 E2E tests, key-pair with passphrase support |
1818
| BigQuery | `@google-cloud/bigquery` | Service Account, ADC | ✅ Live account | 25 E2E tests, UNNEST/STRUCT/DATE types |
1919
| Databricks | `@databricks/sql` | PAT, OAuth | ✅ Live account | 24 E2E tests, Unity Catalog support |
20+
| MongoDB | `mongodb` | Password, Connection String | ✅ Docker | 90 E2E tests, MQL queries, aggregation pipelines |
2021
| Oracle | `oracledb` (thin) | Password | ❌ Needs Oracle 12.1+ | Thin mode only, no Instant Client |
2122

2223
## Installation
@@ -33,6 +34,9 @@ bun add pg # PostgreSQL + Redshift
3334
bun add mysql2 # MySQL
3435
bun add mssql # SQL Server
3536

37+
# Document databases
38+
bun add mongodb # MongoDB
39+
3640
# Cloud warehouses
3741
bun add snowflake-sdk # Snowflake
3842
bun add @google-cloud/bigquery # BigQuery
@@ -129,6 +133,14 @@ altimate-dbt init --project-root /path/to/dbt/project --python-path $(which pyth
129133
|--------|--------------|
130134
| Password | `host`, `port`, `service_name`, `user`, `password` |
131135

136+
### MongoDB
137+
| Method | Config Fields |
138+
|--------|--------------|
139+
| Password | `host`, `port`, `database`, `user`, `password` |
140+
| Connection String | `connection_string: "mongodb://user:pass@host:port/db"` |
141+
142+
MongoDB supports server versions 3.6 through 8.0. Queries use MQL (MongoDB Query Language) via JSON, not SQL. The driver supports `find`, `aggregate`, CRUD operations, index management, and schema introspection via document sampling.
143+
132144
### DuckDB
133145
| Method | Config Fields |
134146
|--------|--------------|
@@ -167,7 +179,7 @@ SSH auth types: `"key"` (default) or `"password"` (set `ssh_password`).
167179

168180
The CLI auto-discovers connections from:
169181

170-
1. **Docker containers**: detects running PostgreSQL, MySQL, MariaDB, SQL Server, Oracle containers
182+
1. **Docker containers**: detects running PostgreSQL, MySQL, MariaDB, SQL Server, Oracle, MongoDB containers
171183
2. **dbt profiles**: parses `~/.dbt/profiles.yml` for all supported adapters
172184
3. **Environment variables**: detects `SNOWFLAKE_ACCOUNT`, `PGHOST`, `MYSQL_HOST`, `MSSQL_HOST`, `ORACLE_HOST`, `DUCKDB_PATH`, `SQLITE_PATH`, etc.
173185

packages/drivers/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"mysql2": "^3.0.0",
1717
"mssql": "^11.0.0",
1818
"oracledb": "^6.0.0",
19-
"duckdb": "^1.0.0"
19+
"duckdb": "^1.0.0",
20+
"mongodb": "^6.0.0"
2021
}
2122
}

packages/drivers/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Re-export types
22
export type { Connector, ConnectorResult, SchemaColumn, ConnectionConfig } from "./types"
33

4-
54
// Re-export config normalization
65
export { normalizeConfig } from "./normalize"
76

@@ -16,3 +15,4 @@ export { connect as connectSqlserver } from "./sqlserver"
1615
export { connect as connectOracle } from "./oracle"
1716
export { connect as connectDuckdb } from "./duckdb"
1817
export { connect as connectSqlite } from "./sqlite"
18+
export { connect as connectMongodb } from "./mongodb"

0 commit comments

Comments
 (0)