Create and Drop collections

Collections એ documents રાખવા માટેનાં MongoDB નાં પાત્રો છે, લગભગ tables જેવાં: createCollection() એને સ્પષ્ટ રીતે બનાવે છે, પણ કોઈ document insert કરવાથી એ આપોઆપ બની જાય છે, અને drop() એને કાઢી નાખે છે.

8 min read · 8 cards · 2 checks

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


Theory

Documents ક્યાં રહે છે

તમારી પાસે festdb database છે અને event ના document નું model પણ છે. પણ documents database માં છૂટાં તરતાં નથી: એ collections માં રહે છે. FestConnect પાસે events collection હશે, કદાચ registrations collection પણ, અને એમ આગળ.

Collection એ BCA205 વાળા relational TABLE નું લગભગ NoSQL વાળું પિતરાઈ છે: એક જ પ્રકારના records રાખવાનું પાત્ર. મોટો ફરક એ કે collection કોઈ schema લાદતું નથી, એટલે એનાં documents માં જુદાં જુદાં fields હોઈ શકે. આ ટૂંકા પાઠમાં collections બનાવવાં અને કાઢવાં જોઈશું, જે આનંદદાયક રીતે સહેલું નીકળે છે, ક્યારેક તો આપોઆપ જ થઈ જાય છે.

Theory

Collection, અને એ table થી કઈ રીતે જુદું પડે છે

Collection એટલે documents નું જૂથ, table ની લગભગ સમકક્ષ વસ્તુ, પણ એક મુક્તિ આપતા ફરક સાથે:

  • Relational table નિશ્ચિત schema લાદે છે: દરેક row માં એ જ columns હોય
  • MongoDB નું collection કોઈ schema લાદતું નથી: એનાં documents માં જુદાં જુદાં fields હોઈ શકે

એટલે FestConnect નું events collection Garba નું document (dress code સાથે), coding-contest નું document (ભાષાઓની list સાથે) અને workshop નું document (સામગ્રીની list સાથે), એ બધાં સાથે રાખી શકે છે, દરેક પોતાના આકારમાં. Collection એમને પ્રકાર પ્રમાણે (એ બધાં events છે એ રીતે) જૂથમાં મૂકે છે, કડક column ગોઠવણી પ્રમાણે નહીં.

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 પહેલાં કદી બનાવ્યું જ નથી. શું થશે?

  1. ભૂલ આવશે: insert કરતાં પહેલાં collection હોવું જ જોઈએ
  2. MongoDB 'events' collection આપોઆપ (આળસુ રીતે) બનાવી દેશે અને document insert કરશે
  3. Document ને રાખવા માટે collection ન હોવાથી એ ખોવાઈ જશે
  4. તમારે હંમેશા પહેલાં db.createCollection ચલાવવું જ પડે
Show the answer

MongoDB 'events' collection આપોઆપ (આળસુ રીતે) બનાવી દેશે અને document insert કરશે

જે નામ અસ્તિત્વમાં ન હોય એમાં તમે પહેલી વાર insert કરો ત્યારે MongoDB એ collection જાતે જ (આળસુ રીતે) બનાવી દે છે, એટલે db.events.insertOne() એક જ પગથિયામાં 'events' બનાવે છે અને document સંઘરે છે: કોઈ ભૂલ નહીં, પહેલેથી કશી તૈયારી નહીં. એટલે જ તમે db.createCollection() ભાગ્યે જ સ્પષ્ટ રીતે બોલાવો છો (એ મુખ્યત્વે capped collection જેવા ખાસ options માટે વપરાય છે). વિકલ્પ A relational ઢબનો 'પહેલાં બનાવવું જ પડે' એવો નિયમ લાદે છે, જે MongoDB માં નથી. વિકલ્પ C એવી data ની ખોટ કલ્પે છે જે થતી જ નથી. વિકલ્પ D અતિશયોક્તિ છે: createCollection મરજિયાત છે. સગવડ એ છે કે document insert કરવું જ એનું collection અસ્તિત્વમાં લાવવા માટે પૂરતું છે.

Think first

તો createCollection સ્પષ્ટ રીતે ક્યારે વાપરવું?

Document insert કરવાથી collection આપોઆપ બની જતું હોય, તો db.createCollection() અસ્તિત્વમાં જ શા માટે છે? વિચારીને પછી tap કરો.

Show the answer

એવા કિસ્સાઓ માટે જ્યાં તમારે અંદર કોઈ data જાય એ પહેલાં જ collection પર OPTIONS ગોઠવવાં હોય. મુખ્ય દાખલો CAPPED collection છે: નિશ્ચિત માપનું collection જે ભરાઈ જાય ત્યારે પોતાનાં સૌથી જૂનાં documents પર આપોઆપ નવું લખી નાખે છે (logs કે ફરતા feed માટે ઉપયોગી). એ વર્તન મેળવવા માટે તમારે capped option સાથે createCollection કરવું જ પડે; insert કરીને એ પાછળથી લગાડી શકાતું નથી. બીજાં options (validation ના નિયમો, ચોક્કસ ગોઠવણો) માટે પણ એ જ રીતે સ્પષ્ટ રચના જોઈએ. એટલે અંગૂઠાનો નિયમ આવો: સામાન્ય collections માટે તો બસ insert કરો અને MongoDB ને આળસુ રીતે એ બનાવવા દો; createCollection() તરફ ત્યારે જ હાથ લંબાવો જ્યારે વાપરતાં પહેલાં collection નું વર્તન ગોઠવવું હોય. મોટા ભાગે આપોઆપ થતો રસ્તો જ પૂરતો છે.

Watch out

Collection ના ફાંદા

Collections પહેલાં બનાવવાં જ પડે એમ માની લેવું: insert કરવાથી એ આપોઆપ બની જાય છે; સ્પષ્ટ createCollection સામાન્ય રીતે જરૂરી નથી.

Schema લદાયેલું હશે એવી અપેક્ષા: collections schema બાબતે લવચીક છે; documents જુદાં હોઈ શકે (તમે validation ના નિયમો ઉમેરી શકો, પણ એ મરજિયાત છે).

drop() પાછું ફેરવી શકાતું નથી: db.events.drop() collection અને એની અંદરનું દરેક document ભૂંસી નાખે છે; ખાતરી કરી લેજો.

જોડણીની ભૂલ નવું collection બનાવી દે છે: db.evnts.insertOne() ચૂપચાપ 'evnts' collection બનાવી દે છે; ખોટી જોડણીવાળા collection ના નામ પર ભૂલ આવતી નથી, નવું (ખોટું) collection બની જાય છે.

Theory

પાત્ર તૈયાર; હવે એને ભરો અને query કરો

Events collection ગોઠવાઈ ગયું (એક જ insert થી બની ગયું), એટલે હવે FestConnect ખરો data સંઘરવાનું અને પાછો મેળવવાનું શરૂ કરી શકે. પછીનો પાઠ MongoDB નું હૃદય છે: CRUD, એટલે કે create (insert), read (find), update અને delete એ ચાર ક્રિયાઓ, સાથે query અને projection operators જે તમને બરાબર જોઈતા events જ લાવવા દે છે. અહીંથી જ MongoDB ખરેખર ઉપયોગી બને છે.

Summary

Key takeaways

  • Collection એટલે documents નું જૂથ: લગભગ table જેવું, પણ કોઈ schema લદાયેલું નહીં (documents જુદાં હોઈ શકે).
  • db.createCollection('events') એને સ્પષ્ટ રીતે બનાવે છે (capped જેવી મરજિયાત ગોઠવણો સાથે).
  • અસ્તિત્વમાં ન હોય એવા collection માં insert કરવાથી એ આપોઆપ (આળસુ રીતે) બની જાય છે: db.events.insertOne() 'events' બનાવી દે છે.
  • show collections એમની યાદી આપે છે; db.events.drop() collection અને એનાં બધાં documents કાઢી નાખે છે.
  • createCollection સ્પષ્ટ રીતે ભાગ્યે જ જોઈએ છે: એને ફક્ત capped collections જેવાં options માટે વાપરો.
  • Collection ના નામમાં જોડણીની ભૂલ ચૂપચાપ નવું (ખોટું) collection બનાવી દે છે, ભૂલ આવતી નથી.
  • યાદ રાખવાની કડી: document insert કરવું જ એનું collection બનાવવા માટે પૂરતું છે.

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

Create and Drop collections · Advance Web Designing (Major-11-01) · Gri-Learn