Thuta Learning
IntermediateDevOps & ToolsIntermediate

Editing Data (Update)

Firebase TutorialLesson 17

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.

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Editing Data (Update) | Thuta Learning