update(...) 단계는 기존 문서를 업데이트합니다.
예
예를 들어 다음 작업은 데이터 모델 변경사항을 컬렉션 그룹의 모든 문서에 백필합니다. 파이프라인은 preferences.color 필드가 누락된 users 컬렉션 그룹의 모든 문서에 해당 필드를 추가합니다.
Node.js
const snapshot = await db.pipeline() .collectionGroup("users") .where(not(exists(field("preferences.color")))) .addFields(constant(null).as("preferences.color")) .removeFields("color") .update() .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Constant, Field, Not snapshot = ( client.pipeline() .collection_group("users") .where(Not(Field.of("preferences.color").exists())) .add_fields(Constant.of(None).as_("preferences.color")) .remove_fields("color") .update() .execute() )
Java
Pipeline.Snapshot snapshot = firestore.pipeline() .collectionGroup("users") .where(not(exists(field("preferences.color")))) .addFields(constant((String) null).as("preferences.color")) .removeFields("color") .update() .execute().get();
Go
snapshot := client.Pipeline(). CollectionGroup("users"). Where(firestore.Not(firestore.FieldExists(firestore.FieldOf("preferences.color")))). AddFields(firestore.Selectables( firestore.ConstantOfNull().As("preferences.color"), )). RemoveFields(firestore.Fields("color")). Update(). Execute(ctx)
행동
모든 데이터 조작 언어 (DML) 단계는 파이프라인의 끝에 와야 합니다.
이 단계로 들어오는 문서에는 업데이트할 문서를 식별하는 __name__ 필드가 포함되어야 합니다. 문서가 없으면 작업이 실패합니다.
대부분의 입력 단계 (collection(...),
collection_group(...),
database(...), and
documents(...))에는 기본적으로 __name__ 필드가 포함됩니다.
문서를 쓰기 직전에 적용할 transformations를 선택적으로 제공할 수 있습니다. 이는 최종 출력 단계 직전에
add_fields(...)를 추가하는 것과 동일하게 작동하며
표현식은 이전 문서의 컨텍스트에서 실행됩니다.
응답에는 수정된 문서 수의 요약이 포함됩니다. 예를 들어 다음 응답은 파이프라인이 세 개의 문서를 수정했음을 확인합니다.
{documents_modified: 3L}
제한사항
DML 단계는 Cloud Firestore Security Rules을 지원하지 않습니다. DML 작업 시도는 Cloud Firestore Security Rules을(를) 통해 거부됩니다.
이 기능의 미리보기 중에는 트랜잭션에서 DML 단계를 실행할 수 없습니다. 일관성 동작에 대한 자세한 내용은 일관성을 참조하세요.
DML 단계 앞의 단계에서 동일한
__name__으로 여러 문서를 생성하는 경우 각 인스턴스가 처리됩니다.update(...)의 경우 동일한 대상 문서가 여러 번 수정될 수 있습니다.delete(...)의 경우 첫 번째 시도 후의 후속 시도는 노옵스(no-ops)입니다.