JSON को Mongoose (JavaScript) स्कीमा में बदलें!

JSON दर्ज करें और इसे Mongoose (JavaScript) स्कीमा में बदलें! Mongoose new Schema, mongoose.model, MongoDB, Node.js समर्थित।
JSON
परिणाम
 

JSON को Mongoose (JavaScript) स्कीमा में बदलें!

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 में मिला दी जाती हैं।

उपयोग कैसे करें

  1. एक document (object) या कई (array) पेस्ट करें। Array में लपेटा गया mongoexport का आउटपुट भी काम करता है।
  2. मॉडल का नाम डालें (default MyModel)। Spaces चल जाते हैं: blog comment BlogComment बनता है।
  3. परिणाम को models/Comment.js जैसी फ़ाइल के रूप में सेव करें और router में const Comment = require('./models/Comment') से load करें।

उदाहरण

इनपुट (JSON)
[
  { "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" }
]
आउटपुट (Mongoose (JavaScript))
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 से मेल खाना ज़रूरी है।

समर्थित सुविधाएँ

  • Field types: संख्याएं → 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 नहीं खोते।
  • Nested objects Schema.Types.Mixed बनते हैं; एक ही तरह की सादी values का array [Schema.Types.String] जैसा typed array बनता है, बाकी कुछ भी [Schema.Types.Mixed]।
  • required: true सिर्फ़ तब जुड़ता है जब हर document में खाली न होने वाली वैल्यू हो। Arrays को यह कभी नहीं मिलता, क्योंकि Mongoose न मौजूद array को [] से भर देता है।
  • Export से कॉपी हुई _id key हटा दी जाती है; उसकी जगह schema _id: { type: Schema.Types.ObjectId, auto: true } declare करता है।
  • जो keys वैध JavaScript identifiers नहीं हैं, जैसे home-url, वे quoted keys के रूप में लिखी जाती हैं।

उपयोग के मामले

Express + MongoDB API की पहली model फ़ाइल

अगर आपने frontend के साथ request body पहले ही तय कर ली है, तो उस JSON को schema draft में बदलें और सीधे अपने POST route के Comment.create(req.body) से जोड़ें।

बिना schema के बढ़ी collection को व्यवस्थित करना

strict: false या native driver से भरी गई collection से कुछ documents निकालें। कौन से fields हमेशा रहते हैं और कौन से कभी-कभी, यह तुरंत required के होने या न होने से दिख जाता है।

नोट्स और सीमाएँ

  • Model mongoose.model('Comment', ...) के रूप में register होता है, और Mongoose collection चुनने के लिए उस नाम को lowercase व बहुवचन करता है: comments। अगर आपकी मौजूदा collection का नाम कुछ और है तो तीसरा argument दें।
  • Mixed fields में अपनी जगह पर हुए बदलाव track नहीं होते; nested वैल्यू edit करने के बाद save() से पहले doc.markModified('profile') call करें, या field को sub-schema से बदलें।
  • Defaults, indexes या unique constraints नहीं बनते। जिन fields से आप खोजते हैं उन पर unique: true या index: true जोड़ें।

अक्सर पूछे जाने वाले प्रश्न

क्या ES module syntax (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 के बिना बनते हैं।