问题描述
我正在尝试使用Spring Boot中的MongoDB反应性模板以反应性方式获取UpdateResult.
I am trying to get an UpdateResult in a reactive way using MongoDB reactive template in Spring Boot.
问题在于更新部分将不会执行,因为我没有订阅它,但是我真的不知道如何执行2个操作并使用反应性范式返回一个值.
The problem is that the update part won't execute as I am not subscribing to it, but I don't really know how to do the 2 operations and returning one value with the reactive paradigm.
这是我正在尝试的:
@GetMapping("\update")
public Mono<UpdateResult> updateTask(@RequestParam(name="taskId") {
Mono<UpdateResult> updateResult = mongoReactiveTemplate
.findById(taskId, Task.class)
.flatmap(object -> mongoReactiveTemplate.updateFirst(Query.query(... the query), new Update().set("something", Task.class)));
return updateresult;
}
flatMap updateFirst不会发生,因为我没有订阅,但是如果我不确定如何返回该值,则需要返回UpdateResult.
The flatMap updateFirst wont happen as I am not subscribing, but I need to return the UpdateResult, if I subscribe im not sure how to return that value.
推荐答案
控制器订阅"流,因此返回Mono<UpdateResult>
非常好,并且可以按预期工作-HTTP客户端将获得序列化的更新结果.
Controller 'subscribes' the stream, so returning Mono<UpdateResult>
is perfectly fine and will work as expected - http client will get the serialized update result.
这篇关于返回Mono< UpdateResult>.使用反应式MongoDB模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!