#1 India's Top IT Training Institute

×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
⚡ FREE · NODE.JS READY

JSON to Mongoose Schema: A MongoDB Model from One Sample Document

Paste a sample document and get a ready Mongoose schema with types, required and timestamps.

  • 🍃 Mongoose model file
  • 🗓️ Dates and ObjectId refs
  • ⏱️ Timestamps option

How to use the JSON to Mongoose Schema Converter

  1. 1. Paste a sample documentOne realistic JSON object, like a document from your collection.
  2. 2. Set optionsModel name, required fields, trimming and timestamps.
  3. 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 a ref guessed 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.

More free tools

See all free tools