Build example document JSON from a JavaScript Mongoose schema defined with new Schema({...}). Because the schema definition object itself is parsed, options like { type: String, enum: [...] } or { type: Map, of: Number } and references to other schema variables are followed.
It helps when documenting collections behind an Express + Mongoose API, or explaining to the frontend what a document looks like. Mongoose-specific rules carry over, such as array subdocuments getting _id while nested paths do not.
Mongoose (JS) box. If sub-schemas live in separate variables, include their definitions in the same input.const variantSchema = new Schema({
sku: String,
price: Schema.Types.Decimal128,
stock: { type: Number, default: 0 },
});
const productSchema = new Schema(
{
title: { type: String, required: true },
variants: [variantSchema],
dimensions: { width: Number, height: Number },
labels: { type: Map, of: String },
},
{ timestamps: { createdAt: "created_at", updatedAt: false } }
);{
"_id": "507f1f77bcf86cd799439011",
"title": "",
"variants": [
{
"_id": "507f1f77bcf86cd799439011",
"sku": "",
"price": {
"$numberDecimal": "0"
},
"stock": 0
}
],
"dimensions": {
"width": 0,
"height": 0
},
"labels": {
"key": ""
},
"created_at": "1970-01-01T00:00:00.000Z",
"__v": 0
}variants: [variantSchema] is an array of subdocuments, so each item gets _id, while __v exists only at the top level. dimensions has no type, so it is a nested path without _id, and Decimal128 appears in its toJSON() form, $numberDecimal. The timestamps option renames the field to created_at and disables updatedAt, leaving a single date field.
_id and __v, honoring versionKey: "rev" renames or false.[subSchema] or [{ name: String }] get _id, unless the schema sets { _id: false }.type (address: { city: String }) stay plain objects with no _id.enum arrays give their first value, { type: Map, of: String } gives {"key": ""}, and Schema.Types.Mixed gives null.Schema.Types.ObjectId becomes a sample ObjectId string and Decimal128 becomes {"$numberDecimal": "0"}.timestamps: true, detailed settings like { createdAt: "created_at", updatedAt: false } are read.null.When a README or wiki shows an example document per collection, generating it from the schema file keeps the docs from drifting away from the code.
Frontends often compare array items without realizing each one carries its own _id. The output makes it clear at which level _id appears.
Fields with ref are plain ObjectId strings until populated. Use the output as the baseline when describing how a response changes after populate().
default ("active", 0, true) is used as the sample value. Function defaults such as Date.now are not run, so those fields get the type's placeholder instead.ref stay ObjectId strings rather than populated objects.new Schema({...}, { _id: false }) written as a field value is converted in place and honors its own options; it does not show up as a separate root entry.toJSON transforms, getters and select: false are not applied.Mongoose's Decimal128 serializes through toJSON() as {"$numberDecimal": "12.50"}. If you send it as a number or string, add a getter or transform to the schema and edit the output to match.
Convert @Schema() classes and interfaces on the Mongoose (TS) to JSON page. This page parses the JavaScript object passed to new Schema().
mongoose.model() is in the file and the schema is elsewhere?The schema definition must be in the input. Paste the new Schema(...) part along with it.