Spring Boot и Mon go - как сделать запрос по вложенному свойству - PullRequest
1 голос
/ 22 января 2020

У меня следующая проблема - как выполнить запрос по вложенному свойству с помощью @Query?

Мой класс продукта (документ в пн go):

@Document(collection = "products")
public class Product {

    @Id
    private String id;

    @DBRef
    private ProductProperties properties;

Как это сделать смотреть в пн go:

{
    "_id" : ObjectId("5d5e78d20e8e3d0006079a84"),
    "companyId" : "1234",
    "properties" : {
        "$ref" : "properties",
        "$id" : ObjectId("5df8dd2331ea7b4a9384335b")
    },
    "calendar" : [ 
        {
            "startDate" : ISODate("2019-09-04T22:00:00.000Z"),
            "endDate" : ISODate("2019-09-09T22:00:00.000Z")
        }
    ],
    "_class" : "org.abc.def"
}

Класс ProductProperties (документ в пн go):

    @Document(collection = "product_properties")
    public class ProductProperties {
        @Id
        private String id;
(...)

Как это выглядит в пн go:

   {
    "_id" : ObjectId("5df8dd2331ea7b4a9384335b"),
    "brand" : "offer Brand_1",
    "model" : "offer model_1",
    "modelNumber" : "offer model number_1",
    "size" : {
    ...
}

Мой репозиторий Spring:

public interface ProductRepository extends MongoRepository<Product, String> {

    @Query("{'properties.id': ?0 }")
    List<Product> findByPropertiesId(String propertiesId);

Я пробовал также:

List<Product> findByProperties_id(String propertiesId)

или

@Query("{'properties.$id': ?0 }")
        List<Product> findByPropertiesId(ObjectId propertiesId);

, но БЕЗ успеха. Знаете ли вы, что не так?

Когда я вызываю:

public List<Product> findProductsByPropertiesId(String properties) {
        if (properties == null) {
            throw new IllegalArgumentException("onFind: propertiesId should not be null.");
        }
        return productRepository.findByProperties_Id(properties);
    } 

Я получаю пустой список: (

Может быть, это невозможно сделать с помощью Query?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...