CRUD operation on firebase dataset

Full CRUD on the Realtime Database combines what you have learned: push to create, a listener to read, setValue or updateChildren on a specific path to update, and removeValue to delete.

11 min read · 7 cards · 2 checks

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


Theory

The four operations, together

You can now write and read the Realtime Database. Putting those together with updating and deleting gives you full CRUD: Create, Read, Update, Delete, everything an app does with data. FestConnect uses all four: add a registration, list registrations, edit an event, cancel a registration.

This lesson assembles CRUD from the pieces you have met and adds the two you have not: updating a specific item and deleting one. The one prerequisite for update and delete is knowing the item's key, so we start there.

Theory

You need the item's key

To update or delete a specific item, you must target its exact path, which means knowing its key. There are two ways to get it: you kept the key returned by push() when you created the item, or you read it from snapshot.key while listening (each child snapshot knows its own key).

With the key, you build a reference to that item, eventsRef.child(key), and act on it. This is why reading often stores each item together with its key: so the UI can later offer edit and delete for that specific entry. No key, no targeted update or delete.

Practical

Update and delete a specific item

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 changes fields; setValue replaces the node

There is an important difference for update. updateChildren(map) changes only the fields you list, leaving the rest of the item intact, so updateChildren(mapOf("seats" to 40)) updates just the seats and keeps the name and date. setValue on the item's node replaces the entire item, so if you setValue only the seats, you would wipe out the other fields.

So to edit a few fields, prefer updateChildren; use setValue only when you truly mean to replace the whole item. Mixing these up is a classic way to accidentally erase data. Update fields with updateChildren; replace nodes with setValue.

Quiz

You want to change only the 'seats' field of an event without affecting its name or date. Which method should you use?

  1. setValue(mapOf("seats" to 40)) on the event's node, because it is simplest
  2. updateChildren(mapOf("seats" to 40)) on the event's node, which changes only that field and leaves the rest intact
  3. removeValue(), because it updates the value
  4. push(), because it edits the item
Show the answer

updateChildren(mapOf("seats" to 40)) on the event's node, which changes only that field and leaves the rest intact

updateChildren changes only the specified fields, so updateChildren(mapOf('seats' to 40)) updates seats while leaving name and date untouched, exactly the goal. Option A is the trap: calling setValue with only the seats on the event's node would REPLACE the entire item, wiping out name and date (setValue overwrites the whole node). Option C, removeValue, DELETES the item, not what you want. Option D, push, CREATES a new item with a new key, it does not edit the existing one. For a partial edit use updateChildren; reserve setValue for replacing the whole node.

Think first

Why does the difference between updateChildren and setValue matter so much?

Both write data. Why is confusing them a serious, data-destroying mistake? Then tap.

Show the answer

Because setValue on a node REPLACES everything at that node, so using it when you meant only a partial edit silently DELETES the fields you did not include, a quiet but destructive data-loss bug. Picture an event stored as { name: 'Robotics', date: '2026-08-01', seats: 50 }. You want to reduce seats to 40. If you call updateChildren(mapOf('seats' to 40)) on the event's node, Firebase merges that change in: seats becomes 40, and name and date stay exactly as they were, correct. But if you call setValue(mapOf('seats' to 40)) on the SAME node, Firebase interprets it as 'the entire event is now just { seats: 40 }', so it overwrites the whole node, and name and date are GONE. Nothing errors; the write succeeds; you have simply destroyed data. This is dangerous precisely because it looks like it should work and fails silently, the kind of bug that corrupts records in production and is hard to trace. The mental model to keep is: setValue means 'this node now equals exactly this', a full replacement, while updateChildren means 'merge these specific changes', a partial update. So for editing a few fields of an existing item, reach for updateChildren; use setValue only when you genuinely intend to replace the whole item (or when creating it fresh). Knowing this distinction protects your data, which is why it is worth drilling. Replace versus merge: choose the one you actually mean.

Summary

Key takeaways

  • Full CRUD on the Realtime Database: Create with push().setValue, Read with a listener, Update on a specific path, Delete with removeValue.
  • To update or delete a specific item you need its key, kept from push() or read from snapshot.key.
  • Build a reference to the item with ref.child(key), then act on it.
  • updateChildren(map) changes only the listed fields, leaving the rest intact.
  • setValue on an item's node replaces the whole item, so it can wipe fields you omit.
  • removeValue() deletes the item entirely.
  • Memory hook: push create, listen read, updateChildren for partial edits (setValue replaces), removeValue delete.

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