5

I'm looking for the correct method name to find all Documents with a given Target's type:

public class Document {

    @Indexed(name = "targets")
    private List<Target> targets;

    public class Target {
        private String value;
        private String type;
    }
}

Unfortunately, all of the following method names aren't working:

List<Document> findByTargetType(...);
List<Document> findByTargetsTargetType(...);
List<Document> findByTargetsContainsTargetType(...);
List<Document> findByTargetsContainsTargetTargetType(...);
List<Document> findByTargetsContainingTargetType(...);
List<Document> findByTargetsContainingTargetTargetType(...);

So which is the correct method name to accomplish the desired feature?

user3105453
  • 1,881
  • 5
  • 32
  • 55
  • https://stackoverflow.com/questions/26684361/filter-child-object-in-spring-data-query – Surely Aug 15 '17 at 08:30
  • Is this about spring-data-jpa or spring-data-mongodb? Please remove the other tag. – Jens Schauder Aug 16 '17 at 05:21
  • @JensSchauder thank's for pointing that out. It's about the naming conventions we have to follow in order to avoid writing queries for JPA or Mongo-Repositories, so it applies to both. How would you suggest to tag it? – user3105453 Aug 17 '17 at 09:07

1 Answers1

7

You can try below query.

Using @Query annotation

@Query("{ 'targets.type' : ?0 }")
List<Document> findByTargetsType(String type);

Or

Using repository supported keywords

List<Document> findByTargetsType(String type);
s7vr
  • 73,656
  • 11
  • 106
  • 127