Atomic Update

first/multi/upsertOne으로 대상 범위를 선택하고 document update 또는 aggregation pipeline update를 명시적으로 구성합니다.


업데이트 범위와 표현을 먼저 선택합니다

text
atomicUpdate()
  -> first() / multi() / upsertOne()
  -> document() / pipeline()
  -> update operations
  -> execute()

Document update

java
Mono<UpdateResult> updated = dsl
    .executeEntity(User.class, MongoTemplateName.FRONT)
    .fields(pair("id", userId))
    .end()
    .atomicUpdate()
    .first()
    .document()
    .inc("loginCount", 1)
    .set("lastLoginAt", Instant.now())
    .execute();

• inc, set, unset, push, addToSet, pull

• Driver update를 그대로 추가: driverUpdate(Bson)

• setOnInsert(...)는 upsertOne().document()에서 사용

Single-document upsert

java
.atomicUpdate()
.upsertOne()
.document()
.set("status", "ACTIVE")
.setOnInsert("createdAt", Instant.now())
.execute();

Pipeline update

java
.atomicUpdate()
.first()
.pipeline()
.set("updatedAt", "$$NOW")
.inc("loginCount", 1)
.nextStage()
.unset("legacyField")
.stage(Aggregates.set(new Field<>("normalized", ...)))
.execute();

nextStage()는 현재 pending $set을 하나의 stage로 flush합니다. Driver-native pipeline stage는 stage(Bson)으로 직접 추가합니다.

Auditing / lifecycle

© 2026 Byeolnaerim. All rights reserved.소개개인정보처리방침