Skip to main content

Overview

The MongoDB schema connector provides migration and introspection support for MongoDB databases. It follows the same SchemaConnector 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

Key characteristics:
  • 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

Connection Parameters

  • authSource: Authentication database (default: database in URI)
  • replicaSet: Replica set name
  • ssl / tls: Enable TLS/SSL
  • retryWrites: Enable retryable writes
  • w: Write concern (e.g., majority)
  • maxPoolSize: Maximum connection pool size
  • minPoolSize: Minimum connection pool size
  • maxIdleTimeMS: Max time connections can remain idle
  • serverSelectionTimeoutMS: Server selection timeout
  • readPreference: 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
Limitations:
  • 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:
Stored as:

Arrays

Native array support for primitives and composite types:

ObjectId

MongoDB’s native identifier type:
The @db.ObjectId attribute maps to MongoDB’s 12-byte BSON ObjectId.

Schema Operations

Database Lifecycle

Create Database:
MongoDB creates databases lazily on first write:
Drop Database:
Drops the entire database and all collections. Reset:
For MongoDB, reset always drops the database (soft reset not supported).

Migration

MongoDB migrations differ from SQL:
Migration Steps:
  • CreateCollection: Create new collection
  • DropCollection: Drop collection
  • CreateIndex: Create index on collection
  • DropIndex: Drop index
  • UpdateValidator: Update schema validation rules
No SQL Scripts:
MongoDB migrations are applied directly via the driver; there’s no SQL-like script format.

Introspection

MongoDB introspection uses sampling to infer schema:
Sampling Process:
  1. Describe collections: List all collections in database
  2. Sample documents: Read sample documents from each collection
  3. Infer schema: Analyze document structure to determine field types
  4. Detect composite types: Identify embedded documents
  5. Find indexes: Read index definitions
  6. Generate model: Create Prisma schema from inferred structure
Implementation:
Challenges:
  • 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):
The collection stores:
  • Migration ID
  • Migration name
  • Checksum
  • Applied timestamp
  • Status

Transient Errors

MongoDB has specific retry logic for transient errors:
Transient errors occur due to:
  • Replica set elections
  • Network hiccups
  • Lock contention
Prisma automatically retries transactions when these occur.

Destructive Change Checking

MongoDB destructive change checker warns about:
Warnings:
  • 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:
These return:

Schema Validation

MongoDB supports JSON schema validation at the database level:
Prisma can update validators during migrations but does not generate them from schema by default.

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:

Client Wrapper

The MongoDB connector uses a client wrapper (client_wrapper.rs):
Error conversion:

Future: Query Compiler Support

Planned MongoDB Query Compiler implementation will require:
  1. Driver adapter: JavaScript/TypeScript adapter for MongoDB Node.js driver
  2. Query translation: Prisma query AST to MongoDB aggregation pipeline
  3. Type mapping: Prisma types to BSON types
  4. Relation resolution: Application-level joins via aggregation $lookup
  5. Transaction support: Multi-document transactions on replica sets
Until then, MongoDB uses the legacy connector architecture.

Next Steps

Connector Overview

Return to connector architecture overview

SQL Connectors

Explore SQL connector implementation