1

I use Spring Data DSL to query MongoDB in the following way:

Page<Member> findByCommunitiesCodeContaining(String code, Pageable pageable);

It returns all Members of a community with a given code.

Problem: I need to pass a collection of community codes and return Members that participate in at least one community from the listed codes (where intersection of communities is not empty).

I browsed the Spring Data Mongo documentation but couldn't find a DSL supporting this case.

Question: How do I query records with intersection of collections?

More details. Here is how my structure looks on Java side.

@Document
public class Member {
    @Id
    private String id;
    private List<Community> communities;
}

Community:

public class Community {
    private String region;
    private String code;
}
Sasha Shpota
  • 9,436
  • 14
  • 75
  • 148
  • 1
    Try `Page findByCommunitiesCodeIn(Collection code, Pageable pageable);` – Valijon May 29 '19 at 14:20
  • 1
    @Valijon thank you. Post it as an answer and I'll accept. – Sasha Shpota May 29 '19 at 23:34
  • 1
    Thank you. Actually, better answer is here: [https://stackoverflow.com/a/30123996/3710490](https://stackoverflow.com/a/30123996/3710490) – Valijon May 30 '19 at 07:54
  • 1
    @Valijon, if I had found that answer I wouldn't have created this question. I would still suggest you to post an answer as there is a big chance that I am not alone. – Sasha Shpota May 30 '19 at 14:28

1 Answers1

-1

One option might be to use a regex

Page<Member> findByCommunitiesCodeRegex(String code, Pageable pageable);

The code should be a regex like:

String code = (code1|code2|code3|....)

Hope it helps, I haven't tried it but it is worth trying.

kimy82
  • 4,069
  • 1
  • 22
  • 25