我正在使用 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 查询可以解决问题,但我不知道如何解决。