CRUD operation on firebase dataset

Realtime Database पर full CRUD वह combine करता है जो आपने सीखा है: create के लिए push, read के लिए एक listener, एक specific path पर update के लिए setValue या updateChildren, और delete के लिए removeValue।

11 min read · 7 cards · 2 checks

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


Theory

चार Operations, साथ

अब आप Realtime Database लिख और पढ़ सकते हैं। इन्हें updating और deleting के साथ जोड़ना आपको full CRUD देता है: Create, Read, Update, Delete, हर वह चीज़ जो एक app data के साथ करती है। FestConnect सभी चार इस्तेमाल करता है: एक registration add करना, registrations list करना, एक event edit करना, एक registration cancel करना।

यह lesson उन pieces से CRUD assemble करता है जो आप मिले हैं और दो जोड़ता है जो आप नहीं मिले: एक specific item update करना और एक delete करना। Update और delete के लिए एक prerequisite है item की key जानना, तो हम वहाँ से शुरू करते हैं।

Theory

आपको Item की Key चाहिए

एक specific item update या delete करने के लिए, आपको इसका exact path target करना पड़ता है, जिसका मतलब है इसकी key जानना। इसे पाने के दो तरीके हैं: item create करते समय push() से return हुई key आपने रखी, या आपने इसे listening करते हुए snapshot.key से पढ़ा (हर child snapshot अपनी own key जानता है)।

Key के साथ, आप उस item का एक reference build करते हैं, eventsRef.child(key), और इस पर act करते हैं। यही वजह है पढ़ना अक्सर हर item को उसकी key के साथ store करता है: तो UI बाद में उस specific entry के लिए edit और delete offer कर सके। No key, no targeted update या delete।

Practical

एक Specific Item Update और Delete करना

val eventsRef = FirebaseDatabase.getInstance().getReference("events")

// UPDATE specific fields without overwriting the whole node:
eventsRef.child(key).updateChildren(mapOf("seats" to 40))

// (or replace the whole item with setValue)
// eventsRef.child(key).setValue(updatedEvent)

// DELETE the item entirely:
eventsRef.child(key).removeValue()

// CREATE was: eventsRef.push().setValue(event)
// READ was:   eventsRef.addValueEventListener(...)

Formula

updateChildren Fields Change करता है; setValue Node Replace करता है

Update के लिए एक important difference है। updateChildren(map) सिर्फ़ आपके listed fields change करता है, बाकी item intact छोड़ते हुए, तो updateChildren(mapOf("seats" to 40)) सिर्फ़ seats update करता है और name और date रखता है। setValue item के node पर पूरे item को replace करता है, तो अगर आप सिर्फ़ seats setValue करते हैं, आप बाकी fields wipe out कर देंगे।

तो कुछ fields edit करने के लिए, updateChildren prefer कीजिए; setValue तभी इस्तेमाल कीजिए जब आप truly पूरे item को replace करना मतलब रखते हों। इन्हें mix करना accidentally data erase करने का एक classic तरीका है। Fields updateChildren से update कीजिए; nodes setValue से replace कीजिए।

Quiz

आप एक event के सिर्फ़ 'seats' field को इसके name या date को affect किए बिना change करना चाहते हैं। आपको कौन सा method इस्तेमाल करना चाहिए?

  1. Event के node पर setValue(mapOf("seats" to 40)), क्योंकि यह simplest है
  2. Event के node पर updateChildren(mapOf("seats" to 40)), जो सिर्फ़ वह field change करता है और बाकी intact छोड़ता है
  3. removeValue(), क्योंकि यह value update करता है
  4. push(), क्योंकि यह item edit करता है
Show the answer

Event के node पर updateChildren(mapOf("seats" to 40)), जो सिर्फ़ वह field change करता है और बाकी intact छोड़ता है

updateChildren सिर्फ़ specified fields change करता है, तो updateChildren(mapOf('seats' to 40)) seats update करता है जबकि name और date untouched रखता है, exactly वह goal। Option A trap है: event के node पर सिर्फ़ seats के साथ setValue call करना पूरे item को REPLACE कर देता, name और date wipe out करते हुए (setValue पूरा node overwrite करता है)। Option C, removeValue, item DELETE करता है, जो आप नहीं चाहते। Option D, push, एक नई key के साथ एक नया item CREATE करता है, यह existing item edit नहीं करता। एक partial edit के लिए updateChildren इस्तेमाल कीजिए; पूरा node replace करने के लिए setValue reserve कीजिए।

Think first

updateChildren और setValue के बीच Difference इतना Matter क्यों करता है?

दोनों data लिखते हैं। इन्हें confuse करना एक serious, data-destroying mistake क्यों है? फिर tap कीजिए।

Show the answer

क्योंकि एक node पर setValue उस node पर सब कुछ REPLACE करता है, तो जब आप सिर्फ़ एक partial edit मतलब रखते थे इसे इस्तेमाल करना silently उन fields को DELETE कर देता है जिन्हें आपने include नहीं किया, एक quiet पर destructive data-loss bug। एक event को { name: 'Robotics', date: '2026-08-01', seats: 50 } की तरह stored imagine कीजिए। आप seats को 40 तक कम करना चाहते हैं। अगर आप event के node पर updateChildren(mapOf('seats' to 40)) call करते हैं, Firebase उस change को merge करता है: seats 40 बनता है, और name और date exactly वैसे ही रहते हैं, correct। पर अगर आप SAME node पर setValue(mapOf('seats' to 40)) call करते हैं, Firebase इसे 'पूरा event अब सिर्फ़ { seats: 40 } है' की तरह interpret करता है, तो यह पूरा node overwrite करता है, और name और date GONE हैं। कुछ भी error नहीं होता; write succeed होती है; आपने बस data destroy कर दिया। यह dangerous exactly इसलिए है क्योंकि यह लगता है यह काम करना चाहिए और silently fail होता है, वह kind का bug जो production में records corrupt करता है और trace करना hard है। जो mental model रखनी है वह है: setValue का मतलब है 'यह node अब exactly यह equals करता है', एक full replacement, जबकि updateChildren का मतलब है 'इन specific changes को merge कीजिए', एक partial update। तो एक existing item की कुछ fields edit करने के लिए, updateChildren के लिए reach कीजिए; setValue तभी इस्तेमाल कीजिए जब आप genuinely पूरे item को replace करना मतलब रखते हों (या इसे fresh create करते समय)। यह distinction जानना आपके data को protect करता है, यही वजह है इसे drill करना worth है। Replace बनाम merge: वह चुनिए जो आप actually मतलब रखते हैं।

Summary

Key takeaways

  • Realtime Database पर full CRUD: push().setValue से Create, एक listener से Read, एक specific path पर Update, removeValue से Delete।
  • एक specific item update या delete करने के लिए आपको इसकी key चाहिए, push() से रखी या snapshot.key से पढ़ी।
  • ref.child(key) से item का एक reference build कीजिए, फिर इस पर act कीजिए।
  • updateChildren(map) सिर्फ़ listed fields change करता है, बाकी intact छोड़ते हुए।
  • एक item के node पर setValue पूरे item को replace करता है, तो यह आपके omitted fields wipe कर सकता है।
  • removeValue() item को पूरी तरह delete करता है।
  • Memory hook: push create करता है, listen read करता है, partial edits के लिए updateChildren (setValue replace करता है), delete के लिए removeValue।

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 Firebase Data storage

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

CRUD operation on firebase dataset · Advance Mobile Application Development - II (Major-15-02) · Gri-Learn