MongoDB Data Modeling In Hindi | MongoDB Schema Design

📔 : MongoDB 🔗

अगर आप ये topic पढ़ रहें हैं तो हम आशा करते हैं कि आप MongoDB के basic concepts को अच्छे से सीख चुके हैं।

किसी भी तरह के application का actual development start करने से पहले store होने वाले data के लिए एक database structure की जरूरत होती है , ताकि आप easily data को process कर पाएं।

क्योंकि आप database design को development के time पर change करेंगे तो ज्यादा problem होगी।

What is data modeling in MongoDB ?

MongoDB में data modeling एक process है जिसमे आप data को NoSQL database के लिए design करते हैं , ताकि applications के specific use cases और requirements को बेहतर तरीके से address किया जा सके।

MongoDB एक NoSQL मतलब unstructured database है , इसका मतलब है कि इसमें traditional relational database systems के जैसे rigid schema नहीं होता और आप अलग - अलग data modeling approaches का use कर सकते हैं data store करने के लिए।

MongoDB Data Modeling Type

नीचे कुछ ऐसे data modeling types हैं जो MongoDB में use किये किये जा सकते हैं , हालाँकि हर type के data model के कुछ advantages और disadvantages भी हैं।

MongoDB Embedded Data Model

इस तरह के model में , data को एक ही document में embed किया जाता है। ये data को उस particular document से related data को उसी document के अंदर store करने लिए सुझाव देता है

For Example

// Inserting an order document with embedded product details.
db.orders.insertOne({
   order_number: "ORD001",
   customer_name: "John Doe",
   products: [
      {
         product_id: 101,
         product_name: "Laptop",
         quantity: 1,
         price: 1000
      },
      {
         product_id: 102,
         product_name: "Mouse",
         quantity: 2,
         price: 20
      }
   ]
})

Example में आप देख सकते हैं कि order से related data को उसी document में embed किया गया है।

Advantages of Embedded Data Model
  • read operations के लिए High performance क्योंकि related data को एक single document में store किया गया है।

  • complex JOIN operations के लिए complex query की जरूरत नहीं पड़ती , जो resource-intensive हो सकता है।

Disadvantages of Embedded Data Model
  • document size Limited होता है , document size 16MB से ज्यादा नहीं सकता है जिससे large application में problem हो सकती है।

  • Data duplication हो सकती है अगर same data को multiple documents में embedded किया जायेगा।

MongoDB Referenced Data Model

इस प्रकार के data model में , data को अलग collection में store किया जाता है और फिर एक reference ID (या किसी unique identifier की help से) का use करके उन documents को associate किया जाता है।

यह model , SQL JOIN की तरह work करता है।

// Inserting product documents
db.products.insertOne({
   _id: 101,
   product_name: "Laptop",
   price: 1000
})

// Inserting order documents
db.orders.insertOne({
   order_number: "ORD001",
   customer_name: "John Doe",
   products: [
      101,  // References to product IDs
      102
   ]
})

इस तरह के model से data fetch करने के लिए aggregate() Method के साथ $lookup Stage का use किया जाता है।

Advantages of Reference Data Model
  • data duplication को काम और storage space को भी reduce करता है।

  • documents के लिए query operations जैसे find और related data को update करना flexible बनाता है।

Disadvantages of Reference Data Model
  • data fetch करने के लिए additional queries ($lookup) का use करना पड़ता है जो performance को impact कर सकता है।

MongoDB Hybrid Data Model

इस model में कुछ data को embed किया जाता है और कुछ data को reference किया जाता है। यह performance और data redundancy के बीच एक balance बनता है।

// Inserting user and post documents
db.users.insertOne({
   _id: 101,
   username: "johndoe"
})

db.posts.insertOne({
   text: "This is a sample post.",
   author_id: 101,
   // Embedding some data
   likes: 50,
   comments: [
      {
         user_id: 102,
         comment_text: "Nice post!"
      }
   ]
})
Advantages of Hybrid Data Model
  • performance और data duplication को reduce करने का अच्छा तरीका हो सकता है।

  • एक flexibility मिलती है कि आप किस data को embed करें और किसे reference करें।

Disadvantages of Hybrid Data Model
  • हालाँकि इसके लिए एक careful planning और design की जरूरत होती है कि किस data को embed करना चाहिए और किसे reference करने चाहिए।

MongoDB Polymorphic Data Model

Polymorphic data model में , एक collection में अलग अलग types के documents को store किया जाता है जिनमे हर एक type का document अपने खुद के fields के साथ होता है।

// Inserting content documents of different types
db.content.insertOne({
   content_type: "article",
   title: "Introduction to MongoDB",
   text: "This is an article about MongoDB..."
})

db.content.insertOne({
   content_type: "video",
   title: "MongoDB Tutorial",
   video_url: "https://www.youtube.com/watch?v=12345"
})
Advantages of Polymorphic Data Model
  • अलग अलग type के documents को same collection में store करने की flexibility provide करता है।

Disadvantages of Polymorphic Data Model
  • complex queries और indexing में challenges हो सकते हैं।

  • additional data validation और error handling की requirement हो सकती है।

MongoDB Time-Series Data Model

Time-series data model में, data को timestamp के store किया जाता है। ये data को समय के अनुकूल रूप में store करने और query करने के लिए optimize किया जाता।

// Inserting sensor readings with timestamps
db.sensor_data.insertOne({
   sensor_id: "sensor-001",
   timestamp: ISODate("2023-09-23T08:00:00Z"),
   temperature: 25.5,
   humidity: 60.2
})
Advantages of Time-series Data Model
  • time-series data को store और query करने के लिए optimized है , जैसे sensor readings, logs, और events.

  • large volumes में time-stamped data को efficiently handle करता है ।

Disadvantages of Time-series Data Model
  • complex, multi-dimensional data models के लिए ज्यादा suitable नहीं है।

  • data को manage करने के लिए specialized indexing और storage strategies की जरूरत पड़ सकती है।

तो ये कुछ data modeling techniques थी जो हम MongoDB में apply कर सकते हैं , I hope आपको समझ आया होगा ~ :)

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers