Theory
Data સાથે થતાં ચાર કામ
દરેક database, પછી એ relational હોય કે NoSQL, data સાથે એ જ 4 કામ કરે છે: Create, Read, Update, Delete, એટલે કે CRUD. BCA205 માં તમે એ SQL થી કર્યાં હતાં (INSERT, SELECT, UPDATE, DELETE); MongoDB એ collection પરની methods થી કરે છે.
મુખ્ય વસ્તુ છે query document: એટલે કે નાનું object જે વર્ણવે છે કે તમને કયાં documents જોઈએ છે. find({ venue: "Ground" }) ને 'venue જેનું Ground હોય એવા events શોધો' એમ વંચાય. Query document અને એના $ વાળા operators પર કાબૂ મેળવો, એટલે FestConnect ને બરાબર જોઈતા events જ મળે. આ પાઠમાં CRUD સાથે query તથા projection નાં ઓજારો છે, જે વાચનને શક્તિશાળી બનાવે છે.
At a glance
MongoDB નું CRUD એક નજરમાં
| ક્રિયા | Method | દાખલો |
|---|---|---|
| Create | insertOne / insertMany | db.events.insertOne({name:"Garba", seats:350}) |
| Read | find / findOne | db.events.find({venue:"Ground"}) |
| Update | updateOne / updateMany | db.events.updateOne({name:"Garba"}, {$set:{seats:300}}) |
| Delete | deleteOne / deleteMany | db.events.deleteOne({name:"Ghost"}) |
Theory
વાચન: query documents અને operators
find(query, projection) એ મુખ્ય કામ કરનારું છે.
Query document ગાળણી કરે છે: find({}) બધું પાછું આપે છે; find({ online: true }) ફક્ત online events પાછા આપે છે. સરખામણી માટે query operators વાપરો (બધા $ થી શરૂ થાય છે):
$gtથી મોટું,$ltથી નાનું,$gteતથા$lte,$neબરાબર નહીં$in(યાદીમાંની કિંમત),$andતથા$or
એટલે find({ seats: { $gt: 100 } }) એવા events શોધે છે જેમાં 100 થી વધુ બેઠકો હોય.
Projection (બીજો argument) નક્કી કરે છે કે કયાં fields પાછાં આવે: find({}, { name: 1, _id: 0 }) ફક્ત name પાછું આપે છે અને _id છુપાવે છે. 1 field ને સામેલ કરે છે, 0 એને બાકાત રાખે છે; _id તમે એને બાકાત ન કરો ત્યાં સુધી મૂળભૂત રીતે સામેલ હોય છે.
Practical
FestConnect ના events પર CRUD (વાચનનું અનુમાન કરો)
// CREATE
db.events.insertMany([
{ name: "Garba Night", seats: 350, online: false },
{ name: "Coding Contest", seats: 60, online: true },
{ name: "Webinar", seats: 500, online: true }
])
// READ: events with more than 100 seats, names only
db.events.find({ seats: { $gt: 100 } }, { name: 1, _id: 0 })
// -> { name: "Garba Night" }, { name: "Webinar" } (Coding Contest has 60)
// UPDATE: $set changes a field (NOT replace the document)
db.events.updateOne({ name: "Garba Night" }, { $set: { seats: 300 } })
// DELETE
db.events.deleteOne({ name: "Coding Contest" })
This example runs in Gri-Learn on the web, where you can edit it and see the output.
Theory
Updates માં $set નો ફાંદો
MongoDB માં CRUD ની સૌથી ખતરનાક ભૂલ એટલે $set વગર update કરવું.
updateOne({ name: "Garba Night" }, { $set: { seats: 300 } }) બરાબર ફક્ત seats બદલે છે અને બીજાં બધાં fields અકબંધ રાખે છે.
પણ updateOne({ name: "Garba Night" }, { seats: 300 }) ($set વગર) મેળ ખાતા આખા document ને { seats: 300 } થી બદલી નાખે છે: name, venue, tags, બીજું બધું ભૂંસાઈ જાય છે, અને હવે document માં ફક્ત seats નું field રહે છે. આ ચૂપચાપ data નાશ કરતી ભૂલ છે. નિયમ: fields બદલવા હોય તો હંમેશા $set જેવો update નો operator વાપરો; ખાલી object આખા document ની જગ્યા લઈ લે છે. બદલવા $set, વધારવા $inc, array માં ઉમેરવા $push.
Quiz
db.events.updateOne({name:"Garba"}, {seats: 300}) $set વગર ચલાવાય છે. Garba ના document નું શું થાય?
- ફક્ત seats નું field 300 થાય છે; બીજાં fields રહી જાય છે
- આખું document {seats: 300} થી બદલાઈ જાય છે: name, venue અને બીજાં બધાં fields ખોવાઈ જાય છે
- એ $set માગતી ભૂલ ફેંકે છે
- કશું બદલાતું નથી
Show the answer
આખું document {seats: 300} થી બદલાઈ જાય છે: name, venue અને બીજાં બધાં fields ખોવાઈ જાય છે
$set જેવા update ના operator વગર MongoDB {seats: 300} ને આખા બદલી નાખતા document તરીકે લે છે, એટલે મેળ ખાતું Garba નું document ફરી લખાઈને એમાં ફક્ત seats જ રહે છે: એનું name, venue, tags અને બીજું બધું નાશ પામે છે. આ કુખ્યાત, ચૂપચાપ data ગુમાવતી ભૂલ છે. વિકલ્પ A એ વર્ણવે છે જે $set કરત ({$set:{seats:300}}), અને લગભગ હંમેશા તમને એ જ જોઈતું હોય છે. વિકલ્પ C ખોટો છે: MongoDB ભૂલ આપતું નથી; એ ચૂપચાપ બદલી નાખે છે, અને એટલે જ આ ભૂલ જોખમી છે. વિકલ્પ D ખોટો છે: એ document ને ખૂબ જ બદલી નાખે છે. આ ફાંદો શીખવે છે તે નિયમ: fields બદલવા હોય તો હંમેશા $set (કે બીજો operator) વાપરો; ખાલી object આખા document ની જગ્યા લઈ લે છે.
Think first
find ને તપાસો
ઉપરના listing માંના insertMany પછી (Garba 350, Coding 60, Webinar 500), db.events.find({ seats: { $gt: 100 } }, { name: 1, _id: 0 }) બરાબર શું પાછું આપશે તે કાઢો. પછી tap કરો.
Show the answer
Query { seats: { $gt: 100 } } એવા events સાથે મેળ ખાય છે જેમાં 100 થી વધુ બેઠકો હોય: Garba Night (350) હા, Coding Contest (60) ના, Webinar (500) હા. એટલે 2 documents મેળ ખાય છે. Projection { name: 1, _id: 0 } ફક્ત name નું field પાછું આપે છે અને _id છુપાવે છે. પરિણામ: { name: "Garba Night" } અને { name: "Webinar" }. Coding Contest ને query બાકાત રાખે છે (60 એ 100 થી વધુ નથી), અને name સિવાયનાં બધાં fields projection છુપાવે છે. MongoDB ના find ને વાંચવું એટલે હંમેશા આ 2 પગથિયાં: query document નક્કી કરે કે કયાં documents, અને projection નક્કી કરે કે એમનાં કયાં fields. બંને તપાસો એટલે output ચોક્કસ મળે.
Watch out
CRUD ના ફાંદા
$set વગર update: આખા document ની જગ્યા લઈ લે છે અને બીજાં fields નાશ કરે છે; હંમેશા $set (કે $inc, $push) વાપરો.
deleteMany({}): ખાલી ગાળણી બધા સાથે મેળ ખાય છે; deleteMany({}) આખું collection સાફ કરી નાખે છે: સાવધ રહો.
updateOne સામે updateMany: updateOne ફક્ત પહેલા મેળને બદલે છે; બધા મેળ માટે updateMany વાપરો.
Projection ભેળવવું: સામાન્ય રીતે તમે કાં તો fields સામેલ કરો (1 થી) કાં બાકાત રાખો (0 થી), બંને નહીં (સામેલ કરવા સાથે _id:0 એ અપવાદ છે).
String ની કિંમતો પર અવતરણ ભૂલી જવાં: find({venue: Ground}) ભૂલ આપે છે; સાચું છે find({venue: "Ground"}).
Theory
CRUD પતી ગયું; હવે પરિણામોને ઘાટ આપો
FestConnect હવે events બનાવી, વાંચી, બદલી અને કાઢી શકે છે, અને query operators તથા projection થી વાચનની ગાળણી પણ કરી શકે છે. Unit 1 નો છેલ્લો પાઠ એથી આગળ જાય છે: projection, updating, પરિણામો મર્યાદિત કરવા અને ક્રમમાં ગોઠવવા માટેના operators, સાથે AGGREGATION ની pipeline, જે data નો સારાંશ કાઢવા માટેનું MongoDB નું ઓજાર છે (event દીઠ નોંધાયેલા લોકોની ગણતરી, બેઠકોનો સરવાળો). પછી unit આ data પર React થી આધુનિક UI બાંધવા તરફ વળે છે.
Summary
Key takeaways
- CRUD: insertOne અને insertMany (create), find અને findOne (read), updateOne અને updateMany (update), deleteOne અને deleteMany (delete).
- Query document વાચનની ગાળણી કરે છે: find({online:true}); query operators $ થી શરૂ થાય છે ($gt, $lt, $ne, $in, $or).
- find({seats:{$gt:100}}) એવા events શોધે છે જેમાં 100 થી વધુ બેઠકો હોય.
- Projection (find નો બીજો argument) fields ચૂંટે છે: {name:1, _id:0} ફક્ત name પાછું આપે છે; 1 સામેલ કરે છે, 0 બાકાત રાખે છે.
- Field બદલવા $set સાથે update કરો; ખાલી object ($set વગર) આખા document ની જગ્યા લઈ લે છે અને બીજાં fields નાશ કરે છે.
- ખાલી ગાળણી વાળું deleteMany({}) બધું જ ભૂંસી નાખે છે: સંભાળીને વાપરો.
- યાદ રાખવાની કડી: query document documents ચૂંટે છે, projection fields ચૂંટે છે, અને $set જગ્યા લીધા વગર બદલે છે.