How to use the JSON to Mongoose Schema Converter
- 1. Paste a sample documentOne realistic JSON object, like a document from your collection.
- 2. Set optionsModel name, required fields, trimming and timestamps.
- 3. Copy the modelGenerate and save it as a .js file in your models folder.
How types are detected
- Text →
String, numbers →Number, true/false →Boolean. - ISO date strings such as 2025-04-01T10:00:00Z →
Date. - 24-character hex strings in fields named _id, id or ending in Id →
Schema.Types.ObjectId, with arefguessed from the name (courseId → 'Course'). - Arrays →
[Type]from their first item; nested objects become nested schema paths. - null →
Mixed, because the real type is unknown.
Fields named email also get lowercase: true, so ASHA@EXAMPLE.COM and asha@example.com don't create two accounts.
Worked example
The sample user document has 8 fields. The generated schema includes joinedAt: { type: Date, required: true }, courseId: { type: mongoose.Schema.Types.ObjectId, required: true, ref: 'Course' }, skills: { type: [String], required: true } and a nested address with city and pin. With timestamps ticked, Mongoose adds createdAt and updatedAt automatically, so there is no need to store them yourself.
The file ends with module.exports = mongoose.model('User', userSchema);. Import it with const User = require('./models/User'); and you can call User.create(...) straight away. Review the result: add unique: true to email, an enum for fixed values like roles, and min or max for numbers. One sample document cannot show every rule your data must follow, so treat the output as a strong first draft.
Frequently asked questions
Why is _id not in the schema?
Mongoose adds _id automatically to every document, so it is skipped.
Can I use ES modules?
Yes. Change require to import mongoose from 'mongoose' and module.exports to export default.
Does it create indexes?
No. Add index: true or unique: true to fields you search or must keep unique.
Is the json to mongoose schema converter free?
Yes, it is free. Generating your first schema asks for your name and WhatsApp number once, and after that the json to mongoose schema converter and every other Uncodemy tool open without the form.