Overview
The MongoDB schema connector provides migration and introspection support for MongoDB databases. It follows the sameSchemaConnector abstraction as SQL connectors but with MongoDB-specific implementation details.
Location: schema-engine/connectors/mongodb-schema-connector/
Query Compiler Status: MongoDB support in the Query Compiler (QC) is not yet implemented. Prisma 7 ships without MongoDB support initially; it will be added once a driver adapter implementation is available.
Architecture
MongoDbSchemaConnector
- Lazy connection initialization via
OnceCell - No connection pooling (MongoDB driver handles internally)
- Supports preview features for experimental functionality
MongoDbSchemaDialect
Supported MongoDB Versions
Provider:mongodb
Supported Versions: MongoDB 4.0+, MongoDB 5.0+, MongoDB 6.0+
Deployment Types:
- Standalone servers
- Replica sets (recommended for transactions)
- Sharded clusters
- MongoDB Atlas
Connection String Format
MongoDB uses the standard MongoDB connection URI:Examples
- Standard
- MongoDB Atlas
- With Options
Connection Parameters
authSource: Authentication database (default: database in URI)replicaSet: Replica set namessl/tls: Enable TLS/SSLretryWrites: Enable retryable writesw: Write concern (e.g.,majority)maxPoolSize: Maximum connection pool sizeminPoolSize: Minimum connection pool sizemaxIdleTimeMS: Max time connections can remain idleserverSelectionTimeoutMS: Server selection timeoutreadPreference: Read preference (primary,secondary, etc.)
Capabilities
MongoDB connector capabilities:- Document model: Store data as BSON documents
- Embedded documents: Nested composite types
- Arrays: Native array support for all types
- Transactions: Multi-document ACID transactions (replica sets/sharded clusters)
- Flexible schema: Schema validation at application level
- Indexes: Single field, compound, multikey, text, geospatial
- No native enums (stored as strings)
- No JOIN operations (handled via application-level aggregation)
- No foreign key constraints (referential integrity at application level)
- No migration scripts (schema is application-defined)
MongoDB-Specific Features
Composite Types
MongoDB excels at embedded documents:Arrays
Native array support for primitives and composite types:ObjectId
MongoDB’s native identifier type:@db.ObjectId attribute maps to MongoDB’s 12-byte BSON ObjectId.
Schema Operations
Database Lifecycle
Create Database:Migration
MongoDB migrations differ from SQL:CreateCollection: Create new collectionDropCollection: Drop collectionCreateIndex: Create index on collectionDropIndex: Drop indexUpdateValidator: Update schema validation rules
Introspection
MongoDB introspection uses sampling to infer schema:- Describe collections: List all collections in database
- Sample documents: Read sample documents from each collection
- Infer schema: Analyze document structure to determine field types
- Detect composite types: Identify embedded documents
- Find indexes: Read index definitions
- Generate model: Create Prisma schema from inferred structure
- Schema inference: MongoDB is schemaless; inference based on samples may not capture all variations
- Type ambiguity: BSON type mapping to Prisma types requires heuristics
- Optional fields: Determining which fields are optional requires statistical analysis
Migration Persistence
MongoDB stores migration records in a collection (equivalent to SQL_prisma_migrations table):
- Migration ID
- Migration name
- Checksum
- Applied timestamp
- Status
Transient Errors
MongoDB has specific retry logic for transient errors:- Replica set elections
- Network hiccups
- Lock contention
Destructive Change Checking
MongoDB destructive change checker warns about:- Dropping collections (data loss)
- Dropping indexes (performance impact)
- Changing field types (potential data loss)
- Adding required fields to existing documents
Limitations
Unsupported Commands
Some schema engine commands are not supported:Schema Validation
MongoDB supports JSON schema validation at the database level:Relations in MongoDB
MongoDB handles relations differently than SQL:Embedded Relations
One-to-many via embedded arrays:Referenced Relations
One-to-many via references:Many-to-Many
Stored as arrays of IDs:Native Types
MongoDB BSON types mapped in Prisma:| Prisma Type | MongoDB Type | Annotation |
|---|---|---|
| String | String | @db.String |
| Int | Int32 | @db.Int |
| BigInt | Int64 | @db.Long |
| Float | Double | @db.Double |
| Decimal | Decimal128 | @db.Decimal |
| Boolean | Bool | @db.Bool |
| DateTime | Date | @db.Date / @db.Timestamp |
| Bytes | BinData | @db.BinData |
| Json | Object/Array | @db.Object / @db.Array |
| String (ID) | ObjectId | @db.ObjectId |
Client Wrapper
The MongoDB connector uses a client wrapper (client_wrapper.rs):
Future: Query Compiler Support
Planned MongoDB Query Compiler implementation will require:- Driver adapter: JavaScript/TypeScript adapter for MongoDB Node.js driver
- Query translation: Prisma query AST to MongoDB aggregation pipeline
- Type mapping: Prisma types to BSON types
- Relation resolution: Application-level joins via aggregation
$lookup - Transaction support: Multi-document transactions on replica sets
Next Steps
Connector Overview
Return to connector architecture overview
SQL Connectors
Explore SQL connector implementation