Paste a document you plan to store in MongoDB and get a CommonJS Mongoose schema file: fields defined with new Schema({...}), the model registered with mongoose.model(), and module.exports at the end. Save it into an Express project's models/ folder and require it as is.
Field paths keep the JSON keys exactly as they are, so the schema reads documents that already exist in the collection without renaming anything. When you paste an array of documents, keys from all of them are merged into one schema.
mongoexport, wrapped in an array, works too.Model name (default MyModel). Spaces are fine: blog comment becomes BlogComment.models/Comment.js and load it in a router with const Comment = require('./models/Comment').[
{ "author": "mina", "body": "Great post!", "likes": 3, "approved": true, "posted_at": "2024-05-01T09:30:00Z" },
{ "author": "leo", "body": "Typo in step 2", "likes": 0, "approved": false, "posted_at": "2024-05-02T11:00:00Z", "reply_to": "6634a1" }
]const mongoose = require('mongoose');
const { Schema } = mongoose;
const commentSchema = new Schema({
_id: { type: Schema.Types.ObjectId, auto: true },
author: { type: Schema.Types.String, required: true },
body: { type: Schema.Types.String, required: true },
likes: { type: Schema.Types.Number, required: true },
approved: { type: Schema.Types.Boolean, required: true },
posted_at: { type: Schema.Types.Date, required: true },
reply_to: { type: Schema.Types.String }
});
const Comment = mongoose.model('Comment', commentSchema);
module.exports = Comment;reply_to exists only on the second comment, so it is generated without required, and posted_at, an ISO date string, becomes Schema.Types.Date. Both keep their snake_case names because the schema paths must match the stored keys.
Schema.Types.Number, true/false → Schema.Types.Boolean, ISO 8601 strings like 2024-05-01 or 2024-05-01T09:30:00Z → Schema.Types.Date, other strings → Schema.Types.String."01234" stay String, so postal codes and phone numbers keep their leading zeros.Schema.Types.Mixed; an array of plain values of one kind becomes a typed array like [Schema.Types.String], anything else [Schema.Types.Mixed].required: true is added only when every document has a non-empty value. Arrays never get it, because Mongoose fills a missing array with []._id key copied from an export is dropped; the schema declares _id: { type: Schema.Types.ObjectId, auto: true } instead.home-url, are written as quoted keys.If you already agreed on a request body with the frontend, turn that JSON into a schema draft and wire it straight into Comment.create(req.body) in your POST route.
Pull a few documents from a collection that was filled with strict: false or the native driver. Which fields always exist and which appear only sometimes shows up immediately as the presence or absence of required.
mongoose.model('Comment', ...), and Mongoose lowercases and pluralizes that name to pick the collection: comments. Pass a third argument if your existing collection uses another name.Mixed fields are not tracked for in-place changes; after editing a nested value call doc.markModified('profile') before save(), or replace the field with a sub-schema.unique constraints are generated. Add unique: true or index: true to fields you look up by.import / export default)?This page only produces CommonJS with require and module.exports. In a "type": "module" project, change the first two lines to import mongoose, { Schema } from 'mongoose' and the last line to export default Comment. For TypeScript or NestJS, use the JSON → Mongoose (TS) page instead.
createdAt and updatedAt?The generated code has no schema options. Add a second argument, new Schema({...}, { timestamps: true }), and Mongoose maintains both fields; remove fields with the same names that came from your JSON.
required field?Mongoose's required validator for strings treats "" as missing. That is why fields that were empty strings in your sample are generated without required in the first place.