Theory
Documents कहाँ रहते हैं
आपके पास festdb database है और एक event document के लिए एक model है। पर documents एक database में loose नहीं तैरते: ये collections में रहते हैं। FestConnect में एक events collection होगी, शायद एक registrations collection, वग़ैरह।
एक collection BCA205 के relational TABLE का roughly NoSQL cousin है: similar kind के records के लिए एक container। बड़ा difference: एक collection एक schema enforce नहीं करती, तो इसके documents की अलग fields हो सकती हैं। यह short lesson collections बनाना और remove करना cover करता है, जो delightfully easy निकलता है, कभी-कभी automatic।
Theory
Collection, और यह Table से कैसे अलग है
एक collection documents का एक group है, table का rough equivalent, एक liberating difference के साथ:
- एक relational table एक fixed schema enforce करता है: हर row की same columns होती हैं
- एक MongoDB collection कोई schema enforce NAHIN करता: इसमें documents की अलग fields हो सकती हैं
तो FestConnect की events collection Garba document (एक dress code के साथ), coding-contest document (एक language list के साथ), और workshop document (एक materials list के साथ), सबको साथ hold कर सकती है, हर एक अपनी own shape के साथ। Collection इन्हें KIND से group करती है (ये सब events हैं), एक rigid column layout से नहीं।
Practical
एक Collection बनाना, दो तरीके, फिर इसे Drop करना
// Explicit creation:
db.createCollection("events")
// OR implicit (lazy) creation: inserting into a non-existent
// collection CREATES it automatically:
db.events.insertOne({ name: "Garba Night", seats: 350 })
// -> if 'events' did not exist, MongoDB just created it.
// List collections in the current database:
show collections
// Drop the collection and ALL its documents:
db.events.drop() // returns true
This example runs in Gri-Learn on the web, where you can edit it and see the output.
Quiz
आप db.events.insertOne({...}) चलाते हैं पर पहले कभी एक 'events' collection create नहीं की। क्या होता है?
- एक error: insert करने से पहले collection exist करनी चाहिए
- MongoDB 'events' collection automatically (lazily) बनाता है और document insert करता है
- Document lose हो जाता है इसे hold करने के लिए कोई collection नहीं होने से
- आपको हमेशा पहले db.createCollection चलाना होगा
Show the answer
MongoDB 'events' collection automatically (lazily) बनाता है और document insert करता है
MongoDB एक collection IMPLICITLY (lazily) बनाता है पहली बार जब आप एक ऐसे नाम में insert करते हैं जो exist नहीं करता, तो db.events.insertOne() 'events' बनाता है और document को एक step में store करता है: कोई error नहीं, कोई prior setup नहीं चाहिए। यही वजह है आप शायद ही कभी db.createCollection() explicitly call करते हैं (आप इसे mainly capped collection जैसे special options के लिए इस्तेमाल करते हैं)। Option A एक relational-style must-create-first rule impose करता है जो MongoDB के पास नहीं है। Option C data loss imagine करता है जो होता ही नहीं। Option D overstate करता है: createCollection optional है। Convenience: एक document insert करना इसकी collection को अस्तित्व में लाने के लिए काफ़ी है।
Think first
आप createCollection Explicitly कब इस्तेमाल करेंगे?
अगर एक document insert करना automatically collection बनाता है, db.createCollection() exist ही क्यों करता है? फिर tap कीजिए।
Show the answer
उन cases के लिए जहाँ आपको collection पर OPTIONS पहले से SET करने हों, किसी भी data जाने से पहले। Main example एक CAPPED collection है: एक fixed-size collection जो full होने पर automatically अपने oldest documents को overwrite करता है (logs या एक rolling feed के लिए useful)। यह behaviour पाने के लिए आपको capped option के साथ createCollection करना पड़ता है; आप इसे insert करके retrofit नहीं कर सकते। दूसरे options (validation rules, specific configurations) similarly explicit creation चाहते हैं। तो rule of thumb: ordinary collections के लिए, बस insert कीजिए और MongoDB को इसे lazily बनाने दीजिए; createCollection() सिर्फ़ तब reach कीजिए जब आपको इस्तेमाल से पहले collection का behaviour configure करना हो। ज़्यादातर समय, automatic route ही सब कुछ है जो आपको चाहिए।
Watch out
Collection Traps
Assume करना कि आपको पहले Collections बनानी पड़ेंगी: insert करना इन्हें automatically बनाता है; explicit createCollection आमतौर पर unnecessary है।
Enforced Schema की Expectation: collections schema-flexible हैं; documents अलग हो सकते हैं (आप validation rules ADD कर सकते हैं, पर वे optional हैं)।
drop() Irreversible है: db.events.drop() collection AND इसमें हर document delete करता है; sure रहिए।
Typos नई Collections बनाते हैं: db.evnts.insertOne() silently एक 'evnts' collection बनाता है; एक misspelt collection name error नहीं देता, यह एक नई (ग़लत) collection बनाता है।
Theory
Container Ready; अब इसे Fill और Query कीजिए
events collection जगह पर होने के साथ (एक single insert से created), FestConnect real data store और retrieve करना शुरू कर सकता है। अगला lesson MongoDB का heart है: CRUD, चार operations create (insert), read (find), update, और delete, plus query और projection operators जो आपको exactly वे events fetch करने देते हैं जो आप चाहते हैं। यहीं MongoDB genuinely useful बनता है।
Summary
Key takeaways
- एक collection documents का एक group है: roughly एक table, पर कोई enforced schema नहीं (documents अलग हो सकते हैं)।
- db.createCollection('events') explicitly एक बनाता है (capped जैसी optional settings के साथ)।
- एक non-existent collection में insert करना इसे AUTOMATICALLY (lazily) बनाता है: db.events.insertOne() 'events' बनाता है।
- show collections इन्हें list करता है; db.events.drop() collection और इसके सारे documents remove करता है।
- आपको शायद ही कभी createCollection explicitly चाहिए: इसे सिर्फ़ capped collections जैसे options के लिए इस्तेमाल कीजिए।
- एक misspelt collection name silently एक नई (ग़लत) collection बनाता है, कोई error नहीं।
- Memory hook: एक document insert करना इसकी collection बनाने के लिए काफ़ी है।