如何改写Uni<表<果>>多没有一个清单? 反应的编程

0

的问题

因为我工作中一个项目,我要重写Uni到多于一个方法"findall"得到所有mongodb文件从一个集合。 我试图重写,但不能够找到一个解决方案

原文:

public Uni<List<Book>> findAll(List<String> authors)
    {

        return getCollection().
                find(Filters.all("authors",authors)).map(Book::from).collectItems().asList();
}

我试过(但不工作)

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

最好的答案

1

我想你使用 ReactiveMongoClient 提供Quarkus. 在这种情况下,你的方法应该是:

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("..."));

}

你不需要做的byFilteringItemsWith,作为一个 Multi 不能包含 null 的项目。

2021-11-24 09:50:36

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................