0

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);
    }
4

1 回答 1

1

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.

于 2021-11-24T09:50:36.497 回答