As i am working in a Project where i want to rewrite the Uni to Multi for a method "findall" to get all the mongodb Document from a collection. I tried to rewrite but not able to find a solution
original:
public Uni<List<Book>> findAll(List<String> authors)
{
return getCollection().
find(Filters.all("authors",authors)).map(Book::from).collectItems().asList();
}
What i tried (But not working)
public Multi<Book> findAll(List<String> authors)
{
return getCollection().find(Filters.all("authors",authors)).transform().
byFilteringItemsWith(Objects::nonNull).onCompletion().ifEmpty().
failWith(new NoSuchElementException("couldn't find the Authors")).onItem().transform(Book::from);
}
I suppose you are using the ReactiveMongoClient provided by Quarkus.
In this case, your method should be:
ReactiveMongoClient client;
public ReactiveMongoCollection<Book> getCollection() {
return client.getDatabase("db").getCollection("books", Book.class);
}
public Multi<Book> findAll(List<String> authors) {
return getCollection()
.find(Filters.all("authors",authors))
.onItem().transform(Book::from)
.onCompletion().ifEmpty()
.failWith(new NoSuchElementException("..."));
}
You don't need to do thebyFilteringItemsWith, as a Multi cannot contain null items.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With