Atomic Update

Select the target range with first/multi/upsertOne and explicitly configure document update or aggregation pipeline update.


First select the update range and expression.

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

• Add Driver update directly: driverUpdate(Bson)

• setOnInsert(...) is used in 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() flushes the current pending $set as a single stage. Driver-native pipeline stages are added directly with stage(Bson).

Auditing / lifecycle

© 2026 Byeolnaerim. All rights reserved.IntroductionPrivacy Policy