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?

3
  • stackoverflow.com/questions/26684361/… Commented Aug 15, 2017 at 8:30
  • Is this about spring-data-jpa or spring-data-mongodb? Please remove the other tag. Commented Aug 16, 2017 at 5: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? Commented Aug 17, 2017 at 9:07

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

1 Comment

findByTargetsType(String type) is what I was looking it. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.