What is FieldValue in Firestore?
Answer
FieldValue (in SDK v9: individual functions) provides special sentinel values for atomic field operations that the server executes: (1) serverTimestamp() — sets the field to the server's current timestamp. Use this instead of new Date() to avoid clock skew across devices; (2) increment(n) — atomically increments (or decrements with negative n) a numeric field. Safe for concurrent updates — use for view counts, like counts, inventory: updateDoc(ref, { viewCount: increment(1) }); (3) arrayUnion(value) — adds elements to an array only if not already present. Safe for concurrent updates — use for tags, category lists; (4) arrayRemove(value) — removes all occurrences of the value from an array; (5) deleteField() — removes a field from the document entirely. These operations are atomic — they don't require a transaction, even under concurrent access, because the server resolves them. Without increment(), counting requires a transaction (read-then-write), which is slower and more prone to contention.