MongoDB में store करने वाला document पेस्ट करें और CommonJS Mongoose schema फ़ाइल पाएं: new Schema({...}) से define किए fields, mongoose.model() से register किया गया model, और अंत में module.exports। इसे Express project के models/ folder में सेव करें और जैसा है वैसा require करें।
Field paths JSON keys को ठीक वैसा ही रखते हैं, इसलिए schema collection में पहले से मौजूद documents को कुछ भी rename किए बिना पढ़ लेता है। Documents का array पेस्ट करने पर सभी की keys एक schema में मिला दी जाती हैं।
mongoexport का आउटपुट भी काम करता है।मॉडल का नाम डालें (default MyModel)। Spaces चल जाते हैं: blog comment BlogComment बनता है।models/Comment.js जैसी फ़ाइल के रूप में सेव करें और router में const Comment = require('./models/Comment') से load करें।[
{ "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 सिर्फ़ दूसरे comment में है, इसलिए वह required के बिना बनता है, और ISO date string posted_at Schema.Types.Date बनता है। दोनों अपने snake_case नाम रखते हैं, क्योंकि schema paths को stored keys से मेल खाना ज़रूरी है।
Schema.Types.Number, true/false → Schema.Types.Boolean, 2024-05-01 या 2024-05-01T09:30:00Z जैसी ISO 8601 strings → Schema.Types.Date, बाकी strings → Schema.Types.String।"01234" जैसे quoted अंक String रहते हैं, इसलिए postal codes और phone numbers अपने आगे के zeros नहीं खोते।Schema.Types.Mixed बनते हैं; एक ही तरह की सादी values का array [Schema.Types.String] जैसा typed array बनता है, बाकी कुछ भी [Schema.Types.Mixed]।required: true सिर्फ़ तब जुड़ता है जब हर document में खाली न होने वाली वैल्यू हो। Arrays को यह कभी नहीं मिलता, क्योंकि Mongoose न मौजूद array को [] से भर देता है।_id key हटा दी जाती है; उसकी जगह schema _id: { type: Schema.Types.ObjectId, auto: true } declare करता है।home-url, वे quoted keys के रूप में लिखी जाती हैं।अगर आपने frontend के साथ request body पहले ही तय कर ली है, तो उस JSON को schema draft में बदलें और सीधे अपने POST route के Comment.create(req.body) से जोड़ें।
strict: false या native driver से भरी गई collection से कुछ documents निकालें। कौन से fields हमेशा रहते हैं और कौन से कभी-कभी, यह तुरंत required के होने या न होने से दिख जाता है।
mongoose.model('Comment', ...) के रूप में register होता है, और Mongoose collection चुनने के लिए उस नाम को lowercase व बहुवचन करता है: comments। अगर आपकी मौजूदा collection का नाम कुछ और है तो तीसरा argument दें।Mixed fields में अपनी जगह पर हुए बदलाव track नहीं होते; nested वैल्यू edit करने के बाद save() से पहले doc.markModified('profile') call करें, या field को sub-schema से बदलें।unique constraints नहीं बनते। जिन fields से आप खोजते हैं उन पर unique: true या index: true जोड़ें।import / export default) मिल सकता है?यह पेज सिर्फ़ require और module.exports वाला CommonJS बनाता है। "type": "module" project में पहली दो lines को import mongoose, { Schema } from 'mongoose' और आखिरी line को export default Comment में बदलें। TypeScript या NestJS के लिए JSON → Mongoose (TS) पेज इस्तेमाल करें।
createdAt और updatedAt कैसे पाऊं?बने हुए कोड में कोई schema options नहीं हैं। दूसरा argument new Schema({...}, { timestamps: true }) जोड़ें, और Mongoose दोनों fields संभालेगा; आपके JSON से आए उसी नाम के fields हटा दें।
required field में खाली string सेव करने पर क्या होता है?Strings के लिए Mongoose का required validator "" को न मौजूद मानता है। इसीलिए आपके sample में खाली string वाले fields शुरू से ही required के बिना बनते हैं।