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.
Mongoose (TS) box, including classes used as subdocuments so arrays and nested objects get filled.@Schema({ timestamps: true, versionKey: false }) and watch createdAt and __v update in the result.@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);{
"_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.
_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.Attachment[], become a one-element array filled from that class.type UserDocument = HydratedDocument<User> are ignored when picking the root, so the real schema class wins.interface declarations without @Schema work too, so a model interface such as IUser can be pasted as is.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.
Tweak the output and pass it to db.messages.insertOne() to seed a local database quickly. Wrap ObjectId strings in ObjectId("...") when you do.
@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.@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.Record<string, any> declared with @Prop({ type: Object }) shows up as {"key": null}.toJSON transforms and select: false are not applied._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.
@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.
new Schema({...}) here?Use the Mongoose (JS) to JSON page for JavaScript schema objects. It reads schema options such as { type: String, enum: [...] } directly.