Thuta Learning
IntermediateDevOps & Toolsintermediate

Editing Data (Update)

Relax. We'll talk through this in plain words — no textbook voice.

Editing Data (Update)

updateDoc is what you use when you only want to change some fields in a document. It's handy for editing a note's title, toggling a pinned state, or updating a profile name.

Code Example

javascript
import { getFirestore, doc, updateDoc, serverTimestamp } from 'firebase/firestore';

const db = getFirestore();

async function updateNote(noteId, newTitle, newContent) {
  const noteRef = doc(db, 'notes', noteId);

  await updateDoc(noteRef, {
    title: newTitle,
    content: newContent,
    updatedAt: serverTimestamp()
  });

  console.log('Note updated');
}

Why use updateDoc?

Because you don't want to replace the whole document — just change a few fields. Existing fields like ownerId and createdAt can be left untouched.

Common mistake

updateDoc can throw an error if the document doesn't exist. If you actually want to create it when it's missing, consider using setDoc with { merge: true } instead.

Editing Data (Update) | Thuta Learning