CRUD operations (Insert, update, delete, find, Query and Projection operators)

MongoDB में CRUD: create के लिए insertOne/insertMany, एक query document के साथ read के लिए find, modify के लिए $set वाला updateOne, remove के लिए deleteOne, plus $gt जैसे query operators और fields चुनने के लिए projection।

12 min read · 10 cards · 2 checks

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


Theory

Data के साथ वो चार चीज़ें जो आप करते हैं

हर database, relational या NoSQL, data के साथ same 4 चीज़ें करता है: Create, Read, Update, Delete: CRUD। आपने ये BCA205 में SQL से किए (INSERT, SELECT, UPDATE, DELETE); MongoDB इन्हें एक collection पर methods से करता है।

Centrepiece query document है: एक छोटा object जो DESCRIBE करता है आपको कौन से documents चाहिए। find({ venue: "Ground" }) इसे पढ़ता है 'events find करो जहाँ venue Ground के equal हो'। Query document और इसके $ operators master कीजिए, और FestConnect exactly वे events fetch कर सकता है जो इसे चाहिए। यह lesson CRUD plus query और projection tools है जो reads को powerful बनाते हैं।

At a glance

एक Glance में MongoDB CRUD

OperationMethodExample
CreateinsertOne / insertManydb.events.insertOne({name:"Garba", seats:350})
Readfind / findOnedb.events.find({venue:"Ground"})
UpdateupdateOne / updateManydb.events.updateOne({name:"Garba"}, {$set:{seats:300}})
DeletedeleteOne / deleteManydb.events.deleteOne({name:"Ghost"})

Theory

Read: Query Documents और Operators

find(query, projection) workhorse है।

Query document filter करता है: find({}) ALL return करता है; find({ online: true }) सिर्फ़ online events return करता है। Comparisons के लिए, query operators इस्तेमाल कीजिए (सब $ से शुरू होते हैं):

  • $gt greater than, $lt less than, $gte/$lte, $ne not equal
  • $in (list में value), $and / $or

तो find({ seats: { $gt: 100 } }) उन events को find करता है जिनकी seats 100 से MORE हैं।

Projection (second argument) select करता है कौन सी FIELDS वापस आती हैं: find({}, { name: 1, _id: 0 }) सिर्फ़ name return करता है, _id hide करते हुए। 1 एक field include करता है, 0 exclude करता है; _id default से included है जब तक आप इसे exclude न करें।

Practical

FestConnect Events पर CRUD (Reads Predict कीजिए)

// 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 Trap

MongoDB में सबसे dangerous CRUD mistake $set के WITHOUT update करना है।

updateOne({ name: "Garba Night" }, { $set: { seats: 300 } }) correctly सिर्फ़ seats बदलता है, हर दूसरी field intact छोड़ते हुए।

पर updateOne({ name: "Garba Night" }, { seats: 300 }) ($set के बिना) पूरे matched document को { seats: 300 } से REPLACE करता है: name, venue, tags: बाकी सब कुछ DELETED हो जाता है, और अब document में सिर्फ़ एक seats field है। यह एक silent data-destroying bug है। Rule: fields बदलने के लिए, हमेशा $set जैसा एक update OPERATOR इस्तेमाल कीजिए; एक bare object पूरे document को replace करता है। Modify करने के लिए $set, increment करने के लिए $inc।

Quiz

db.events.updateOne({name:"Garba"}, {seats: 300}) $set के WITHOUT चलाया जाता है। Garba document का क्या होता है?

  1. सिर्फ़ seats field 300 में बदलती है; बाकी fields रहती हैं
  2. पूरा document {seats: 300} से REPLACE हो जाता है: name, venue और बाकी सारी fields lost हो जाती हैं
  3. यह एक error throw करता है $set चाहते हुए
  4. कुछ नहीं बदलता
Show the answer

पूरा document {seats: 300} से REPLACE हो जाता है: name, venue और बाकी सारी fields lost हो जाती हैं

$set जैसे update operator के बिना, MongoDB {seats: 300} को एक पूरे REPLACEMENT document की तरह treat करता है, तो matched Garba document overwrite होकर सिर्फ़ seats contain करता है: इसका name, venue, tags और बाकी सब कुछ destroy हो जाता है। यह एक notorious silent data-loss bug है। Option A बताता है $set क्या करता ({$set:{seats:300}}), जो लगभग हमेशा आप चाहते हैं। Option C ग़लत है: MongoDB error NAHIN देता; यह quietly replace करता है, यही bug को dangerous बनाता है। Option D false है: यह document को बहुत ज़्यादा बदलता है। यह trap जो rule burn करता है: fields modify करने के लिए, हमेशा $set (या कोई दूसरा operator) इस्तेमाल कीजिए; एक bare object पूरा document replace करता है।

Think first

Find को Trace कीजिए

Listing में insertMany के बाद (Garba 350, Coding 60, Webinar 500), exactly work out कीजिए db.events.find({ seats: { $gt: 100 } }, { name: 1, _id: 0 }) क्या return करता है। फिर tap कीजिए।

Show the answer

Query { seats: { $gt: 100 } } उन events को match करता है जिनकी seats 100 से MORE हैं: Garba Night (350) YES, Coding Contest (60) NO, Webinar (500) YES। तो 2 documents match होते हैं। Projection { name: 1, _id: 0 } सिर्फ़ name field return करता है और _id hide करता है। Result: { name: "Garba Night" } और { name: "Webinar" }। Coding Contest query से exclude हो जाता है (60, 100 से > नहीं है), और name के अलावा हर field projection से hidden है। एक MongoDB find पढ़ना हमेशा ये 2 steps हैं: query document decide करता है WHICH documents, projection decide करता है इनकी WHICH fields। दोनों trace कीजिए और output exact होता है।

Watch out

CRUD Traps

$set के बिना Update: पूरे document को replace करता है, बाकी fields destroy करते हुए; हमेशा $set (या $inc, $push) इस्तेमाल कीजिए।

deleteMany({}): एक empty filter EVERYTHING match करता है; deleteMany({}) collection को wipe कर देता है: careful रहिए।

updateOne बनाम updateMany: updateOne सिर्फ़ FIRST match बदलता है; सारे matches के लिए updateMany इस्तेमाल कीजिए।

Projection Mixing: आप आमतौर पर या तो fields include करते हैं (1s) या exclude (0s), दोनों नहीं (सिवाय includes के साथ _id:0 के)।

String Values पर Quotes भूलना: find({venue: Ground}) error देता है; यह find({venue: "Ground"}) है।

Theory

CRUD हो गया; अब Results Shape कीजिए

FestConnect अब events create, read, update और delete कर सकता है, और query operators और projection से reads filter कर सकता है। Final Unit 1 lesson आगे जाता है: projection, updating, limiting और sorting results के लिए operators, plus AGGREGATION pipeline, data summarise करने के लिए MongoDB का tool (हर event के लिए registrants count करना, seats total करना)। फिर unit React के साथ इस data पर modern UI build करने shift करता है।

Summary

Key takeaways

  • CRUD: insertOne/insertMany (create), find/findOne (read), updateOne/updateMany (update), deleteOne/deleteMany (delete)।
  • Query document reads filter करता है: find({online:true}); query operators $ से शुरू होते हैं ($gt, $lt, $ne, $in, $or)।
  • find({seats:{$gt:100}}) उन events को find करता है जिनकी seats 100 से ज़्यादा हैं।
  • Projection (find का 2nd argument) fields select करता है: {name:1, _id:0} सिर्फ़ name return करता है; 1 include करता है, 0 exclude करता है।
  • एक field बदलने के लिए $set से update कीजिए; एक bare object (कोई $set नहीं) पूरे document को REPLACE करता है, बाकी fields destroy करते हुए।
  • एक empty filter वाला deleteMany({}) सब कुछ delete करता है: careful रहिए।
  • Memory hook: query document documents पिक करता है, projection fields पिक करता है, $set replace किए बिना बदलता है।

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

CRUD operations (Insert, update, delete, find, Query and Projection operators) · Advance Web Designing (Major-11-01) · Gri-Learn