MongoDB exporter documentation
Overview
MongoDBExporter Convert the JSON Schema generated by schema-dsl into MongoDB's $jsonSchema validation format, which can be directly used for document validation when creating a collection.
Core functions
- ✅ Convert to MongoDB
$jsonSchemaformat - ✅ Automatic mapping type is BSON type
- ✅ Keep all constraints
- ✅ Generate
createCollectioncommand - ✅Support strict/loose validation mode
quick start
Output:
API reference
Constructor
Parameters:
method
export(jsonSchema)
Convert JSON Schema to MongoDB Validation Schema.
Parameters:
jsonSchema(Object): JSON Schema object generated by schema-dsl
Return Value:
Object: MongoDB authentication object containing$jsonSchema
generateCreateCommand(collectionName, jsonSchema)
Generate createCollection command object.
Parameters:
collectionName(string): collection namejsonSchema(Object): JSON Schema object
Return Value:
generateCommand(collectionName, jsonSchema)
Generate an executable MongoDB command string.
Output:
MongoDBExporter.export(jsonSchema) (static method)
Fast export without instantiation.
Configuration options
Authentication mode
Complete example
User set validation
Used in MongoDB
type mapping
constraint mapping
Export restrictions
⚠️ IMPORTANT: Not all schema-dsl features can be exported to a database schema.
Exported features not supported:
- ❌ Conditional validation logic (
s.match(),s.if()) - ❌ Custom validator (
.custom()) - ❌ Complex JSON Schema keywords (
allOf,anyOf,oneOf) - ❌ Custom error message (
.messages())
Detailed instructions: Please read Export restrictions document
Related documents
Corresponding sample file
Example entry: mongodb-exporter.ts
Description: Covers $jsonSchema export, generateCreateCommand() and generateCommand(), corresponding to the validation set creation scenario in the document.