MongoDB Datatypes (String, Integer, Boolean, Double, Arrays, Objects); database creation and dropping database

एक MongoDB document typed fields (string, int, double, boolean, array, embedded object) वाला एक JSON-like object है, और use dbname lazily एक database बनाता है, dropDatabase() इसे remove करता है।

10 min read · 9 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

एक Event, एक Document की तरह

MongoDB में, FestConnect की Garba Night एक table में एक row नहीं है: यह एक document है, एक JSON-like object जिसे आप लगभग aloud पढ़ सकते हैं:

{ name: "Garba Night", seats: 350, online: false, tags: ["cultural", "dance"] }

एक name (string), seats (number), online (boolean), tags (एक array): हर field अपना खुद का type carry करती है, और पूरी चीज़ exactly BCA405-01 के JavaScript objects जैसी दिखती है। यह lesson वे data TYPES cover करता है जो एक document hold कर सकता है, और इन्हें store करने वाला DATABASE कैसे create और drop करें। पहले, vocabulary: databases collections hold करते हैं, collections documents hold करते हैं।

Theory

MongoDB की Structure और Types

MongoDB 3 levels nest करता है: एक database collections hold करता है, और एक collection documents (individual records) hold करता है।

एक document की fields ये types हो सकती हैं:

  • String: "Garba Night"
  • Integer और Double: whole numbers और decimals (350, 49.5)
  • Boolean: true / false
  • Array: एक list hold करती एक field (["cultural", "dance"])
  • Object (embedded document): एक field जो एक ANOTHER document hold करती है
  • plus ObjectId (automatic unique _id), Date, और Null

वह embedded-object ability powerful है: एक event का venue event के अंदर एक पूरा sub-document हो सकता है, कोई join नहीं चाहिए।

Practical

एक FestConnect Event Document (हर Type)

// A single MongoDB document for one event:
{
  name: "Garba Night",          // String
  seats: 350,                   // Integer
  fee: 49.5,                    // Double
  online: false,                // Boolean
  tags: ["cultural", "dance"],  // Array
  venue: {                      // Object (embedded document)
    hall: "Main Ground",
    capacity: 400
  }
  // _id: an ObjectId is added automatically if you do not supply one
}
// The venue is embedded INSIDE the event: no separate table, no join.

This example runs in Gri-Learn on the web, where you can edit it and see the output.

Theory

एक Database बनाना और Drop करना

Mongo shell में, आप एक database के साथ इन commands से काम करते हैं:

  • `use festdb`: festdb database पर SWITCH कीजिए, इसे LAZILY बनाते हुए। Crucial nuance: database actually disk पर तब तक नहीं लिखा जाता जब तक आप इसमें data insert न करें। एक brand-new नाम पर use बस आपको इसकी तरफ़ point करता है
  • `show dbs`: existing databases list कीजिए (एक lazily-created empty एक अभी नहीं दिखेगा)
  • `db.dropDatabase()`: CURRENT database को पूरी तरह delete कीजिए

Lazy creation beginners को surprise करता है: वे use newdb करते हैं, show dbs चलाते हैं, और इसे नहीं देखते, सोचते हैं यह fail हो गया। यह नहीं हुआ: database तब materialise होता है जब पहला document इसमें land करता है।

Quiz

आप एक database name पर `use festdb` चलाते हैं जो अभी exist नहीं करता, फिर `show dbs`, और festdb LIST नहीं है। क्यों?

  1. use command fail हो गया
  2. MongoDB databases LAZILY बनाता है: festdb तब तक disk पर नहीं लिखा जाता जब तक आप data insert न करें, तो यह तब तक show dbs में नहीं दिखता
  3. आपको पहले createDatabase() चलाना होगा
  4. Database names में letters नहीं हो सकते
Show the answer

MongoDB databases LAZILY बनाता है: festdb तब तक disk पर नहीं लिखा जाता जब तक आप data insert न करें, तो यह तब तक show dbs में नहीं दिखता

MongoDB databases (और collections) LAZILY बनाता है: use festdb आपके context को festdb पर switch करता है और इसे prepare करता है, पर यह तब तक persisted या listed नहीं होता जब तक पहला document insert न हो। तो एक empty, freshly-used database को show dbs में listed न देखना expected है, error नहीं। Option A normal behaviour को failure की तरह misread करता है। Option C एक command invent करता है: कोई createDatabase() नहीं है; use plus एक insert वह तरीका है जिससे एक database अस्तित्व में आता है। Option D false है। याद रखने वाला rule: use आपको एक database की तरफ़ point करता है (इसे lazily बनाते हुए), और यह truly तभी exist करता है जब इसमें data हो।

Think first

Embedded Object या Separate Collection?

FestConnect के event में एक hall name और capacity वाला एक venue है। आप venue को event document के अंदर embed कर सकते थे, या venues को एक separate collection में store कर सकते थे। Embedding कब जीतता है? फिर tap कीजिए।

Show the answer

Venue को event के अंदर EMBED कीजिए जब venue data उस event का HOLD करता है और इसके साथ ही access होता है: जो आमतौर पर यहाँ case है। Embedding मतलब एक document read event AND इसका venue दोनों पाता है, कोई join नहीं: fast और simple, और यह MongoDB के document model को fit करता है। आप इसकी बजाय एक SEPARATE collection तब इस्तेमाल करेंगे जब same venue कई events के across SHARED हो और आप इसे एक ही जगह update करना चाहें (हर event में venue details duplicate करने से बचते हुए), या जब venue data बड़ा हो और rarely चाहिए हो। यह embed-बनाम-reference decision MongoDB का normalise-बनाम-denormalise trade-off का version है: साथ accessed और parent-owned data के लिए embed कीजिए, shared या independently-changing data के लिए reference कीजिए।

Watch out

MongoDB Basics Traps

एक Empty Database के Appear होने की Expectation रखना: use lazily create करता है; db data insert होने के बाद ही दिखता है।

createDatabase() ढूँढना: कोई नहीं है; use + insert इसे बनाता है।

Automatic _id भूलना: हर document को एक unique ObjectId _id मिलता है जब तक आप अपना खुद न दें।

dropDatabase() CURRENT db Drop करता है: sure रहिए आप सही एक पर हैं (db से check कीजिए) drop करने से पहले: यह irreversible है।

Database, Collection, Document Confuse करना: database collections hold करता है collections documents (records) hold करते हैं।

Theory

Database Ready; अब Collections

आप एक event को एक typed document की तरह model कर सकते हैं और festdb database create या drop कर सकते हैं। Documents COLLECTIONS के अंदर रहते हैं (tables का rough equivalent), तो अगला short lesson collections create और drop करना cover करता है, unit के heart से पहले: CRUD operations (insert, find, update, delete) और query और projection operators जो MongoDB को FestConnect के लिए genuinely useful बनाते हैं।

Summary

Key takeaways

  • MongoDB structure: एक database collections hold करता है, एक collection documents (JSON-like records) hold करता है।
  • Document field types: String, Integer, Double, Boolean, Array, Object (embedded document), plus ObjectId, Date, Null।
  • Embedded objects related data (एक venue) को एक document के अंदर बैठने देते हैं: कोई join नहीं चाहिए।
  • हर document को एक automatic unique _id (एक ObjectId) मिलता है जब तक आप एक न दें।
  • use dbname एक database पर switch (और lazily create) करता है; data insert होने तक यह persisted नहीं होता।
  • db.dropDatabase() current database delete करता है (irreversible: check कीजिए आप सही एक पर हैं)।
  • Memory hook: documents types वाला JSON हैं; use lazily create करता है, data इसे real बनाता है।

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Concepts of NoSQL: MongoDB

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati