Updating an object using mongodb and kotlin spring data does not work

I have the following request handler

fun x(req: ServerRequest) = req.toMono()
    .flatMap {
        ...
        val oldest = myRepository.findOldest(...) // this is the object I want to modify
        ...
        val v= anotherMongoReactiveRepository.save(Y(...)) // this saves successfully
        myRepository.save(oldest.copy(
                remaining = (oldest.remaining - 1)
        )) // this is not saved
        ok().body(...)
    }

and the next mongodb reactive repository

@Repository
interface MyRepository : ReactiveMongoRepository<X, String>, ... {
}

The problem is that after the method is executed, the save()object does not change. I managed to solve the problem with save().block(), but I donโ€™t know why the first save to another repository works, but itโ€™s not. Why is it required block()?

+4
source share
1 answer

, - . , block(). , Mono/Flux, map(), flatMap(),... Mono/Flux . Spring Mono/Flux . . ( block()).

MongoDB Java:

@GetMapping("/users")
public Mono<User> getPopulation() {
    return userRepository.findOldest()
            .flatMap(user -> {              // process the response from DB
                user.setTheOldest(true);
                return userRepository.save(user);
            })
            .map(user -> {...}); // another processing
}
+1

Source: https://habr.com/ru/post/1694593/


All Articles