언어, 프레임워크/JS
addDoc()과 setDoc()
go_getter
2025. 2. 23. 01:18
1. addDoc
- Firestore database의 컬렉션에 새로운 문서 추가
- 자동으로 고유한 문서 ID를 생성하고, 그 안에 데이터 저장
import { collection, addDoc } from "firebase/firestore";
const docRef = await addDoc(collection(db, "collectionName"), {
name: "홍길동",
comment: "좋은 댓글입니다!"
});
2. setDoc
- Firestore database의 컬렉션에 새로운 문서 추가
- 사용자가 문서 ID 지정
- 문서 ID를 사용자가 알기 때문에, 기존 문서 업데이트 가능
import { doc, setDoc } from "firebase/firestore";
const docRef = doc(db, "collectionName", "customId123"); // customId123 문서 ID를 사용
await setDoc(docRef, {
name: "홍길동",
comment: "좋은 댓글입니다!"
});