Process Flow - Increment operations
Start with initial value
Read current value
Add increment amount
Write new value back
Operation complete
Increment operations read a value, add a number, then save the new value back.
const increment = firebase.firestore.FieldValue.increment(1); db.collection('counters').doc('pageViews').update({ count: increment });
| Step | Action | Current Value | Increment Amount | New Value | Result |
|---|---|---|---|---|---|
| 1 | Read 'count' field | 5 | N/A | N/A | Current value is 5 |
| 2 | Apply increment | 5 | 1 | 6 | Add 1 to 5 equals 6 |
| 3 | Write new value | N/A | N/A | 6 | Updated 'count' to 6 |
| 4 | Operation complete | 6 | N/A | 6 | Increment operation finished |
| Variable | Start | After Step 1 | After Step 2 | After Step 3 | Final |
|---|---|---|---|---|---|
| count | 5 | 5 | 5 | 6 | 6 |
| incrementAmount | 1 | 1 | 1 | 1 | 1 |
Increment operations in Firebase:
- Use FieldValue.increment(amount) to add atomically.
- No need to read value manually.
- Handles missing fields as zero.
- Updates happen server-side safely.
- Example: update({count: increment(1)}) adds 1.