SpringBoot ReactiveMongoTemplate 部分更新文档
SpringBoot ReactiveMongoTemplate updating document partially
我正在开发一个 kotlin 反应式 spring-boot mongodb 项目。我正在尝试更新文档,但效果不佳。
我的问题与 Whosebug 中的以下问题非常相似。
Spring reactive mongodb template update document partially with objects
所以我在 mongo
中有一个文档
{
"id": 1,
"name": "MYNAME",
"email": "MYEMAIL",
"encryptedPassword": "12345",
...........................
}
当我使用以下 header
之一在 uri localhost:8080/user/1
上调用 PATCH 时
{
"name": "NEW NAME"
}
{
"email": "NEW EMAIL"
}
我只想用收到的字段更新我的文档。
我的处理程序代码
fun update(serverRequest: ServerRequest) =
userService
.updateUser(serverRequest.pathVariable("id").toLong(), serverRequest.bodyToMono())
.flatMap {
ok().build()
}
我的服务实现代码
override fun updateUser(id: Long, request: Mono<User>): Mono<UpdateResult> {
val changes = request.map { it -> PropertyUtils.describe(it) }
val updateFields: Update = Update()
changes.subscribe {
for (entry in it.entries) {
updateFields.set(entry.key, entry.value)
}
}
return userRepository.updateById(id, updateFields)
}
我的存储库代码
fun updateById(id: Long, partial: Update) = template.updateFirst(Query(where("id").isEqualTo(id)), partial, User::class.java)
我的用户代码
@Document
data class User(
@Id
val id: Long = 0,
var name: String = "",
val email: String = "",
val encryptedPassword: ""
)
我已经听从了Spring reactive mongodb template update document partially with objects的建议。
我的代码确实更新了,但它更新到我的 User
class.
的初始构造函数
有人可以帮忙吗?
我想您应该将此问题视为在 Java/Kotlin 中修补对象的一般问题。我找到了一篇关于此的文章:https://cassiomolin.com/2019/06/10/using-http-patch-in-spring/#json-merge-patch。即使您不会部分更新对象,也不会对应用程序的性能产生如此大的影响。
我想出了如何部分更新我的数据。
首先我将正文请求更改为字符串。 (使用 bodyToMono(String::class.java
)
然后我将更改后的JSON字符串更改为JSONObject(org.json
).
我为每个 JSONObject
的键创建了 Update
,这将是更新我的实体的部分数据。
以下是我的实现方式。
override fun updateUser(id: Long, request: Mono<String>): Mono<UpdateResult> {
val update = Update()
return request.map { JSONObject(it) }
.map {
it.keys().forEach { key -> update.set(key, it[key]) }
update
}
.flatMap { it -> userRepository.updateById(id, it) }
}
如果您有更多 'cleaner' 方法来做到这一点,请分享更多想法。谢谢
我正在开发一个 kotlin 反应式 spring-boot mongodb 项目。我正在尝试更新文档,但效果不佳。
我的问题与 Whosebug 中的以下问题非常相似。
Spring reactive mongodb template update document partially with objects
所以我在 mongo
中有一个文档{
"id": 1,
"name": "MYNAME",
"email": "MYEMAIL",
"encryptedPassword": "12345",
...........................
}
当我使用以下 header
之一在 urilocalhost:8080/user/1
上调用 PATCH 时
{
"name": "NEW NAME"
}
{
"email": "NEW EMAIL"
}
我只想用收到的字段更新我的文档。
我的处理程序代码
fun update(serverRequest: ServerRequest) =
userService
.updateUser(serverRequest.pathVariable("id").toLong(), serverRequest.bodyToMono())
.flatMap {
ok().build()
}
我的服务实现代码
override fun updateUser(id: Long, request: Mono<User>): Mono<UpdateResult> {
val changes = request.map { it -> PropertyUtils.describe(it) }
val updateFields: Update = Update()
changes.subscribe {
for (entry in it.entries) {
updateFields.set(entry.key, entry.value)
}
}
return userRepository.updateById(id, updateFields)
}
我的存储库代码
fun updateById(id: Long, partial: Update) = template.updateFirst(Query(where("id").isEqualTo(id)), partial, User::class.java)
我的用户代码
@Document
data class User(
@Id
val id: Long = 0,
var name: String = "",
val email: String = "",
val encryptedPassword: ""
)
我已经听从了Spring reactive mongodb template update document partially with objects的建议。
我的代码确实更新了,但它更新到我的 User
class.
有人可以帮忙吗?
我想您应该将此问题视为在 Java/Kotlin 中修补对象的一般问题。我找到了一篇关于此的文章:https://cassiomolin.com/2019/06/10/using-http-patch-in-spring/#json-merge-patch。即使您不会部分更新对象,也不会对应用程序的性能产生如此大的影响。
我想出了如何部分更新我的数据。
首先我将正文请求更改为字符串。 (使用 bodyToMono(String::class.java
)
然后我将更改后的JSON字符串更改为JSONObject(org.json
).
我为每个 JSONObject
的键创建了 Update
,这将是更新我的实体的部分数据。
以下是我的实现方式。
override fun updateUser(id: Long, request: Mono<String>): Mono<UpdateResult> {
val update = Update()
return request.map { JSONObject(it) }
.map {
it.keys().forEach { key -> update.set(key, it[key]) }
update
}
.flatMap { it -> userRepository.updateById(id, it) }
}
如果您有更多 'cleaner' 方法来做到这一点,请分享更多想法。谢谢