Convert Mongoose (TS) schemas to JSON!

Input Mongoose (TypeScript) or NestJS schemas to generate example JSON! Handy for API tests and mock request bodies.
Mongoose (TS)
Result
 

Convert Mongoose (TS) schemas to JSON!

Give it @Schema() classes written with NestJS's @nestjs/mongoose, or plain TypeScript interfaces, and it produces an example of the document as toJSON() would export it. _id, __v and the timestamp fields are added following Mongoose's own rules, even when the class does not declare them.

Use it to pin down response DTOs in a NestJS service, or to agree with your team on what test documents in a collection should look like. Unlike the JS page, values are taken from TypeScript property types rather than @Prop() options.

How to use

  1. Paste the schema class file into the Mongoose (TS) box, including classes used as subdocuments so arrays and nested objects get filled.
  2. The class nobody else references becomes the root; make sure your main document class is the one in the output.
  3. Change options like @Schema({ timestamps: true, versionKey: false }) and watch createdAt and __v update in the result.

Example

Input (NestJS · @nestjs/mongoose)
@Schema({ _id: false })
export class Attachment {
  @Prop() url: string;
  @Prop() size: number;
}

@Schema({ timestamps: true, versionKey: false })
export class Message {
  @Prop({ type: Types.ObjectId, ref: "Room", required: true })
  roomId: Types.ObjectId;

  @Prop({ required: true })
  text: string;

  @Prop({ type: [Attachment], default: [] })
  attachments: Attachment[];

  @Prop()
  readAt?: Date;
}

export const MessageSchema = SchemaFactory.createForClass(Message);
Output (JSON)
{
  "_id": "507f1f77bcf86cd799439011",
  "roomId": "507f1f77bcf86cd799439011",
  "text": "",
  "attachments": [
    {
      "url": "",
      "size": 0
    }
  ],
  "readAt": "1970-01-01T00:00:00.000Z",
  "createdAt": "1970-01-01T00:00:00.000Z",
  "updatedAt": "1970-01-01T00:00:00.000Z"
}

Message references Attachment, so Message is the root. _id is prepended, timestamps: true adds two date fields, and versionKey: false removes __v. roomId, typed Types.ObjectId, becomes a sample ObjectId string.

Supported features

  • The root document gets _id (sample ObjectId 507f1f77bcf86cd799439011) and, unless versionKey: false, __v: 0; a string versionKey renames it.
  • @Schema({ timestamps: true }) appends createdAt and updatedAt as ISO strings.
  • Types.ObjectId and MongooseSchema.Types.ObjectId become ObjectId strings; Types.Decimal128 and other numeric types become 0.
  • Arrays of another class, such as Attachment[], become a one-element array filled from that class.
  • Type aliases like type UserDocument = HydratedDocument<User> are ignored when picking the root, so the real schema class wins.
  • Plain interface declarations without @Schema work too, so a model interface such as IUser can be pasted as is.

Use cases

NestJS responses and Swagger

When a service returns this.model.find().lean() directly, this shows the exact keys clients receive, including the _id and __v that leak into the response.

Test documents for mongosh

Tweak the output and pass it to db.messages.insertOne() to seed a local database quickly. Wrap ObjectId strings in ObjectId("...") when you do.

Notes and limitations

  • Nested classes decorated with @Schema() are treated as subdocuments: they get their own _id (unless @Schema({ _id: false })) and timestamps, but never __v. Nested classes without @Schema stay plain objects.
  • A literal @Prop({ default: ... }) is used as the sample value; required and enum options are not read. For a fixed set of values, declare a TypeScript literal union and its first value is used.
  • A Record<string, any> declared with @Prop({ type: Object }) shows up as {"key": null}.
  • Virtuals, toJSON transforms and select: false are not applied.

FAQ

My API renames _id to id, but the output shows _id.

The output follows Mongoose's default toJSON(). If your schema adds id through toJSON: { virtuals: true } or a transform, rename the key in the result.

What if the type is only in the options, like @Prop({ type: [String] })?

Values come from the property's declared type. With tags: string[] you get [""]; with no TypeScript type at all, the value is unknown and becomes null.

Can I convert a JavaScript new Schema({...}) here?

Use the Mongoose (JS) to JSON page for JavaScript schema objects. It reads schema options such as { type: String, enum: [...] } directly.