Cập nhật nguyên tử
Chọn phạm vi mục tiêu bằng first/multi/upsertOne và cấu hình rõ ràng cập nhật tài liệu hoặc cập nhật pipeline tổng hợp.
Chọn phạm vi và biểu thức cập nhật trước.
text
atomicUpdate()
-> first() / multi() / upsertOne()
-> document() / pipeline()
-> update operations
-> execute()Cập nhật tài liệu
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
• Thêm cập nhật Driver trực tiếp: driverUpdate(Bson)
• setOnInsert(...) được sử dụng trong upsertOne().document()
Cập nhật một tài liệu duy nhất
java
.atomicUpdate()
.upsertOne()
.document()
.set("status", "ACTIVE")
.setOnInsert("createdAt", Instant.now())
.execute();Cập nhật Pipeline
java
.atomicUpdate()
.first()
.pipeline()
.set("updatedAt", "$$NOW")
.inc("loginCount", 1)
.nextStage()
.unset("legacyField")
.stage(Aggregates.set(new Field<>("normalized", ...)))
.execute();nextStage() xả $set đang chờ hiện tại thành một stage. Stage pipeline Driver-native được thêm trực tiếp bằng stage(Bson).