I have the following problem - how to query by nested property with @Query
?
My Product class (document in Mongo):
@Document(collection = "products")
public class Product {
@Id
private String id;
@DBRef
private ProductProperties properties;
How does it look in Mongo:
{
"_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 class (document in Mongo):
@Document(collection = "product_properties")
public class ProductProperties {
@Id
private String id;
(...)
How does it look in Mongo:
{
"_id" : ObjectId("5df8dd2331ea7b4a9384335b"),
"brand" : "offer Brand_1",
"model" : "offer model_1",
"modelNumber" : "offer model number_1",
"size" : {
...
}
My Spring repository:
public interface ProductRepository extends MongoRepository<Product, String> {
@Query("{'properties.id': ?0 }")
List<Product> findByPropertiesId(String propertiesId);
I've tried also:
List<Product> findByProperties_id(String propertiesId)
or
@Query("{'properties.$id': ?0 }")
List<Product> findByPropertiesId(ObjectId propertiesId);
but WITHOUT success. DO you know what's wrong?
When I invoke:
public List<Product> findProductsByPropertiesId(String properties) {
if (properties == null) {
throw new IllegalArgumentException("onFind: propertiesId should not be null.");
}
return productRepository.findByProperties_Id(properties);
}
I get empty list:(
Maybe there is impossible to do that via Query?