spring-projects / spring-data-examples

Spring Data Example Projects
Apache License 2.0
5.17k stars 3.39k forks source link

how to return differnt things when insert #638

Closed Storm-Born closed 2 years ago

Storm-Born commented 2 years ago

how to writing the code which can return diffenent value such as true or false base on the operation result when using mongodb react api to insert an object into database.

the code below may describe my wish, but it was not true, some methods are not exist.

mongo.insert(data).Onsuccess(return true).onError(return false).

mp911de commented 2 years ago

Project Reactor provides a pretty extensive guide explaining which operator to use. You can achieve what you're looking for through the following operators:

.thenReturn(true).onErrorResume(e -> Mono.just(false))

Taking a step back, the way you're describing the issue indicates that you're trying to apply imperative patterns using reactive programming. You should familiarize yourself with Reactive Streams and the signal types (completion, error signals) to use the intrinsics of the programming model for chaining operations and handling errors.