0

我正在使用 Panache 存储库策略将数据保存在 MongoDB 数据库中。

@ApplicationScoped
public class ProductRepository implements PanacheMongoRepository<Product> {}

假设我有一个包含以下数据的集合。

|id  | name | price |
|1   | p1   | $ 1.00|
|2   | p2   | $ 2.00|
|3   | p3   | $ 3.00|
|4   | p4   | $ 4.00|

我有一个List<ObjectId> productsToRetrieve[1, 3]

查询的预期结果是:

ID 姓名 价格
1 p1 1.00 美元
3 p3 3.00 美元

这是我尝试过的

find("id", productsToRetrieve);
find("id = ?1", productsToRetrieve);
list("id", productsToRetrieve);
Document query = new Document();
find(new Document("id", new Document("$in", productsId))).list();

我可以使用 aforEach从存储库中检索每个产品并添加到列表中,但这不是一个好方法。PanacheQL 查询可以解决问题,但我不知道如何解决。

4

1 回答 1

0

要获得所需的输出,必须使用$in. 这里的例子。

并且使用 quarkus,根据docs,它说:

?1 中的状态将映射到 {'status':{$in: [?1]}}

所以必要的查询是:

_id in ?1, ?3
于 2021-02-16T12:46:52.017 回答