Bird
Raised Fist0
Firebasecloud~20 mins

Writing data (set, update, push) in Firebase - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Firebase Writing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ service_behavior
intermediate
2:00remaining
What happens when using set() with merge option?
Consider this Firebase Realtime Database operation:

ref.set({name: 'Alice'}, {merge: true})

What is the effect of using set with the merge: true option?
AIt throws an error because set() does not support a merge option in Realtime Database.
BIt updates only the specified fields in the existing data, preserving other fields.
CIt appends the new object as a child node under the reference.
DIt replaces the entire data at the reference with the new object, removing any existing fields.
Attempts:
2 left
💡 Hint
Think about the difference between set() and update() in Firebase Realtime Database.
❓ Configuration
intermediate
2:00remaining
Which code correctly pushes a new item to a list in Firebase Realtime Database?
You want to add a new message to a list of messages stored under /chat/messages. Which code snippet correctly pushes a new message with text 'Hello'?
Afirebase.database().ref('/chat/messages').push({text: 'Hello'})
Bfirebase.database().ref('/chat/messages').set({text: 'Hello'})
Cfirebase.database().ref('/chat/messages').update({text: 'Hello'})
Dfirebase.database().ref('/chat/messages').child('text').push('Hello')
Attempts:
2 left
💡 Hint
Remember that push() creates a unique key for each new item in a list.
❓ Architecture
advanced
2:00remaining
How to atomically update multiple fields in Firebase Realtime Database?
You want to update the user's name and age at /users/user123 in a single operation without overwriting other data. Which approach achieves this atomically?
AUse push() with an object containing name and age fields.
BUse set() with an object containing only name and age fields.
CUse update() with an object containing name and age fields.
DUse set() with merge option set to true.
Attempts:
2 left
💡 Hint
Think about which method updates specific fields without replacing the entire node.
❓ security
advanced
2:00remaining
What happens if a user tries to push data without write permission in Firebase Realtime Database?
Assuming Firebase Realtime Database rules deny write access to a user at /posts, what happens when the user runs ref.push({title: 'New Post'})?
AThe data is pushed successfully and visible to all users.
BThe data is pushed but only visible to the user who pushed it.
CThe push() call silently fails without error.
DThe push() call throws a permission denied error.
Attempts:
2 left
💡 Hint
Consider how Firebase enforces security rules on write operations.
✅ Best Practice
expert
3:00remaining
Which approach minimizes data loss risk when updating nested data in Firebase Realtime Database?
You need to update a deeply nested field /users/user123/profile/age without affecting other profile data. Which method is best to minimize risk of data loss?
AUse set() on <code>/users/user123/profile</code> with the entire profile object including the updated age.
BUse update() on <code>/users/user123/profile</code> with only the age field.
CUse set() on <code>/users/user123/profile/age</code> with the new age value.
DUse push() on <code>/users/user123/profile/age</code> with the new age value.
Attempts:
2 left
💡 Hint
Think about partial updates and avoiding overwriting unrelated data.

Practice

(1/5)
1. Which Firebase method would you use to completely replace all data at a specific database path?
easy
A. push()
B. update()
C. set()
D. get()

Solution

  1. Step 1: Understand set() behavior

    The set() method writes data to a specified path and replaces any existing data there.
  2. Step 2: Compare with other methods

    update() modifies specific fields without replacing all data, push() adds new child nodes, and get() reads data.
  3. Final Answer:

    set() -> Option C
  4. Quick Check:

    Replace all data = set() [OK]
Hint: Use set() to overwrite all data at a path [OK]
Common Mistakes:
  • Confusing update() with set() and expecting full replacement
  • Using push() when needing to replace data
  • Thinking get() writes data
2. Which of the following is the correct syntax to add a new child with a unique key under the path 'messages' using Firebase Realtime Database?
easy
A. firebase.database().ref('messages').set({ text: 'Hello' })
B. firebase.database().ref('messages').push({ text: 'Hello' })
C. firebase.database().ref('messages').update({ text: 'Hello' })
D. firebase.database().ref('messages').get({ text: 'Hello' })

Solution

  1. Step 1: Identify method for unique child addition

    The push() method adds a new child with a unique key under the specified path.
  2. Step 2: Check syntax correctness

    Using push({ text: 'Hello' }) under 'messages' correctly adds a new message with unique key.
  3. Final Answer:

    firebase.database().ref('messages').push({ text: 'Hello' }) -> Option B
  4. Quick Check:

    Unique child addition = push() [OK]
Hint: Use push() to add unique child nodes [OK]
Common Mistakes:
  • Using set() which overwrites all children
  • Using update() which only modifies existing fields
  • Using get() which only reads data
3. Given the following Firebase code snippet:
firebase.database().ref('users/user1').update({ age: 30 });

What will happen to the existing data at 'users/user1' if it currently contains { name: 'Alice', age: 25 }?
medium
A. Only the 'age' field is updated to 30; 'name' remains 'Alice'
B. The entire 'users/user1' data is replaced with { age: 30 }
C. A new child with key 'age' is added under 'user1'
D. An error occurs because update() cannot be used here

Solution

  1. Step 1: Understand update() behavior

    The update() method modifies only the specified fields without removing other existing data.
  2. Step 2: Apply to given data

    Since 'age' is updated to 30, 'name' remains unchanged as 'Alice'.
  3. Final Answer:

    Only the 'age' field is updated to 30; 'name' remains 'Alice' -> Option A
  4. Quick Check:

    update() changes fields, keeps others [OK]
Hint: update() changes fields, keeps others intact [OK]
Common Mistakes:
  • Thinking update() replaces all data like set()
  • Confusing push() with update()
  • Expecting an error when update() is valid
4. You want to update the user's email without removing other user data. Which Firebase method usage below contains an error?
medium
A. firebase.database().ref('users/user1').update({ email: 'new@example.com' })
B. firebase.database().ref('users/user1/email').set('new@example.com')
C. firebase.database().ref('users/user1').update({ email: 'new@example.com', phone: '1234567890' })
D. firebase.database().ref('users/user1').push({ email: 'new@example.com' })

Solution

  1. Step 1: Understand push() usage

    push() adds a new child with a unique key; it does not update existing fields.
  2. Step 2: Analyze the goal

    To update email without removing other data, update() is correct; push() wrongly adds a new child instead.
  3. Final Answer:

    Using push() to update email is incorrect -> Option D
  4. Quick Check:

    push() adds new child, not update fields [OK]
Hint: Use update() to change fields, not push() [OK]
Common Mistakes:
  • Using push() to update existing fields
  • Using set() which overwrites all data
  • Confusing update() and push() roles
5. You want to add multiple chat messages under 'chats' in Firebase Realtime Database, each with a unique key, and also update the user's last active timestamp without overwriting existing data. Which combination of methods should you use?
hard
A. Use push() for messages and update() for last active timestamp
B. Use push() for messages and set() for last active timestamp
C. Use update() for messages and set() for last active timestamp
D. Use set() for messages and update() for last active timestamp

Solution

  1. Step 1: Add multiple messages with unique keys

    Use push() under 'chats' to add each message with a unique key without overwriting others.
  2. Step 2: Update last active timestamp without overwriting

    Use update() on the user's data to change the timestamp without removing other fields.
  3. Final Answer:

    Use push() for messages and update() for last active timestamp -> Option A
  4. Quick Check:

    push() adds unique children, update() modifies fields [OK]
Hint: push() for new items, update() for field changes [OK]
Common Mistakes:
  • Using set() for messages which overwrites all chats
  • Using set() for timestamp which removes other user data
  • Using update() to add unique children
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard