Spring Boot mongoRepository查询

时间:2019-03-22 17:45:38

标签: java mongodb spring-boot

我有此数据[ { "id": "3", "name": "Lakshya2", "email": "lakshya.punhani2@gmail.com", "products": [ { "id": "2", "description": "qwerty", "rate": "20", "name": "AC" } ] }, { "id": "1", "name": "Lakshya", "email": "lakshya.punhani@gmail.com", "products": [ { "id": "3", "description": "qwerty", "rate": "20", "name": "AC" }, { "id": "2", "description": "desc", "rate": "10", "name": "BBG" } ] } ]

我也是spring boot和mongo db的新手,所以我只需要知道如何通过用户ID和产品ID获取特定产品。

这是我尝试过的方法,但是它提供了错误的数据

@Repository
public interface UserRepository extends MongoRepository<User, String>
{   
    Product findByproducts_productId(String productId);
}

1 个答案:

答案 0 :(得分:0)

我认为您可以使用以下查询。

@Query(value =“ {'users.id':?0,'products.id':?1}”) 列出findByproductsByUserIdAndproductId(String userId,String productId);