2

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?

Matley
  • 1,953
  • 4
  • 35
  • 73
  • Here are couple of answers with related questions. See [spring data - Mongodb - findBy Method for nested objects](https://stackoverflow.com/questions/12730370/spring-data-mongodb-findby-method-for-nested-objects) and [MongoDb Spring find in nested object](https://stackoverflow.com/questions/34016419/mongodb-spring-find-in-nested-object). – prasad_ Jan 22 '20 at 03:13
  • Unfortunately I still didn't solve my problem. I think I tried all options... without success:( – Matley Jan 22 '20 at 22:30
  • Those will not work in your case. You are using [@DBRef](https://docs.spring.io/spring-data/data-mongodb/docs/current/api/org/springframework/data/mongodb/core/mapping/DBRef.html) annotation in the `Product` class. See this post on using the `@DBRef`: [Spring Mongo DB @DBREF](https://stackoverflow.com/questions/44259583/spring-mongo-db-dbref). What is a database reference? See [DBRefs](https://docs.mongodb.com/manual/reference/database-references/index.html) in MongoDB docs. – prasad_ Jan 23 '20 at 09:54
  • Still doesn't work. I added "properties" : { "$ref" : "properties", "$id" : ObjectId("5df8dd2331ea7b4a9384335b") }, to my JSON. My repository looks like @Query(value = "{'properties.id': ?0 }") List findByPropertiesId(String propertiesId); – Matley Jan 23 '20 at 22:10
  • _"Still doesn't work..."_: Please post the exact data and the code you are working with in the post. Mention what is the error or problem you are facing, clearly. – prasad_ Jan 24 '20 at 01:39
  • @prasad_ Everything is written in this post above. What else should I add? I don't get any error, just invoking productRepository.findByProperties_Id(properties) returns empty List. See my updated content above, I added calling a repository function – Matley Jan 25 '20 at 19:52
  • 1
    I don't know what the issue is. There are quite a few related posts both on StackOverflow as well as other sites. Please try searching for _"Spring MongoDB @DBRef"_. A suggestion only: one way to solve the issue is to try and run an example on your own _and_ that way you can start with working code and further apply it to your situation. – prasad_ Jan 26 '20 at 13:17
  • Yes, I know is it a good approach... But I think I tried everything... But when you say it's possible to find by nested object id then I can't give up... I have to find a solution – Matley Jan 29 '20 at 22:19
  • @Matley Did you find any solution to your problem? If yes, could you please share it with us? – user1419243 Jan 15 '21 at 08:34

1 Answers1

0
@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.findByPropertiesId(new ObjectId(properties));
}
  • 1
    Please take a minute to edit your answer to include some explanation, instead of just a code block. This will help the OP and future readers understand the _why_ and not just the _how_. Such questions tend to prove more useful over time—and are more likely to be recognized with upvotes. – Jeremy Caney Jul 26 '21 at 00:47