इसे NestJS के @nestjs/mongoose से लिखी @Schema() classes या सादे TypeScript interfaces दें, और यह document का वह उदाहरण बनाता है जैसा toJSON() export करेगा। _id, __v और timestamp fields Mongoose के अपने नियमों से जोड़े जाते हैं, भले ही class उन्हें declare न करे।
इसे NestJS service में response DTOs पक्के करने, या टीम के साथ यह तय करने में इस्तेमाल करें कि collection के test documents कैसे दिखने चाहिए। JS पेज के विपरीत, values @Prop() options से नहीं बल्कि TypeScript property types से ली जाती हैं।
Mongoose (TS) बॉक्स में पेस्ट करें, subdocuments के रूप में इस्तेमाल होने वाली classes समेत, ताकि arrays और nested objects भरें।@Schema({ timestamps: true, versionKey: false }) जैसे options बदलें और परिणाम में createdAt और __v को अपडेट होते देखें।@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 Attachment को reference करता है, इसलिए Message root है। _id सबसे आगे जुड़ता है, timestamps: true दो date fields जोड़ता है, और versionKey: false __v हटाता है। Types.ObjectId type वाला roomId sample ObjectId string बनता है।
_id (sample ObjectId 507f1f77bcf86cd799439011) और, अगर versionKey: false न हो, __v: 0 मिलता है; string versionKey इसे rename करता है।@Schema({ timestamps: true }) createdAt और updatedAt को ISO strings के रूप में जोड़ता है।Types.ObjectId और MongooseSchema.Types.ObjectId ObjectId strings बनते हैं; Types.Decimal128 और दूसरे numeric types 0 बनते हैं।Attachment[], उस class से भरा एक element वाला array बनते हैं।type UserDocument = HydratedDocument<User> जैसे type aliases अनदेखे होते हैं, इसलिए असली schema class जीतती है।@Schema के बिना सादे interface declarations भी काम करते हैं, इसलिए IUser जैसा model interface जैसा है वैसा पेस्ट किया जा सकता है।जब कोई service सीधे this.model.find().lean() लौटाती है, तो यह दिखाता है कि clients को ठीक कौन सी keys मिलती हैं, response में आ जाने वाले _id और __v समेत।
आउटपुट में थोड़ा बदलाव करके उसे db.messages.insertOne() में दें और local database जल्दी भरें। ऐसा करते समय ObjectId strings को ObjectId("...") में लपेटें।
@Schema() लगी nested classes subdocuments मानी जाती हैं: उन्हें अपना _id (जब तक @Schema({ _id: false }) न हो) और timestamps मिलते हैं, लेकिन __v कभी नहीं। @Schema के बिना nested classes सादे objects रहती हैं।@Prop({ default: ... }) sample वैल्यू के रूप में इस्तेमाल होता है; required और enum options नहीं पढ़े जाते। तय values के सेट के लिए TypeScript literal union declare करें, और उसकी पहली वैल्यू इस्तेमाल होगी।@Prop({ type: Object }) से declare किया Record<string, any> {"key": null} के रूप में दिखता है।toJSON transforms और select: false लागू नहीं होते।_id को id में rename करता है, लेकिन आउटपुट _id दिखाता है।आउटपुट Mongoose के default toJSON() का पालन करता है। अगर आपका schema toJSON: { virtuals: true } या transform से id जोड़ता है, तो परिणाम में key rename करें।
@Prop({ type: [String] }), तो?Values property के declared type से आती हैं। tags: string[] से [""] मिलता है; अगर कोई TypeScript type ही नहीं है, तो वैल्यू अज्ञात है और null बनती है।
new Schema({...}) बदल सकते हैं?JavaScript schema objects के लिए Mongoose (JS) to JSON पेज इस्तेमाल करें। वह { type: String, enum: [...] } जैसे schema options सीधे पढ़ता है।