问题
I am trying to get an UpdateResult in a reactive way using MongoDB reactive template in Spring Boot.
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.
This is what I am trying:
@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;
}
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.
回答1:
Controller 'subscribes' the stream, so returning Mono<UpdateResult>
is perfectly fine and will work as expected - http client will get the serialized update result.
来源:https://stackoverflow.com/questions/57019089/returning-monoupdateresult-with-reactive-mongodb-template