Atomare Aktualisierung
Wählen Sie den Zielbereich mit first/multi/upsertOne und konfigurieren Sie das Dokumenten-Update oder die Aggregationspipeline-Update explizit.
Wählen Sie zuerst den Update-Bereich und die Darstellung aus.
text
atomicUpdate()
-> first() / multi() / upsertOne()
-> document() / pipeline()
-> update operations
-> execute()Dokumentaktualisierung
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
• Fügen Sie das Driver-Update direkt hinzu: driverUpdate(Bson)
• setOnInsert(...) wird in upsertOne().document() verwendet.
Single-document upsert
java
.atomicUpdate()
.upsertOne()
.document()
.set("status", "ACTIVE")
.setOnInsert("createdAt", Instant.now())
.execute();Pipeline-Aktualisierung
java
.atomicUpdate()
.first()
.pipeline()
.set("updatedAt", "$$NOW")
.inc("loginCount", 1)
.nextStage()
.unset("legacyField")
.stage(Aggregates.set(new Field<>("normalized", ...)))
.execute();nextStage() spült das aktuelle ausstehende $set in eine Stage. Driver-native Pipeline-Stages werden direkt mit stage(Bson) hinzugefügt.