0

What would be an easy way to perform the SQL query below in Spring Data MongoDB? Doesn't matter whether it's using MongoRepository, Query, or Criterion.

SELECT * FROM Person WHERE firstname like '%tom%' OR lastname like '%tom%'

isoplayer
  • 67
  • 2
  • 7
  • https://stackoverflow.com/a/41749367/7975771 does that help you? – varman Sep 28 '20 at 03:21
  • @varman Almost, but the "or" part is tricky and I haven't found any solutions without having to go to the DB twice (2 queries basically) – isoplayer Sep 28 '20 at 04:19

1 Answers1

0

You can try something like following with MongoRepository.

List<User> users = userRepository.findByFirstnameRegexOrLastnameRegex(".*john.*",".*smith.*");

interface UserRepository extends MongoRepository<User, String> {
     List<User> findByFirstnameRegexOrLastnameRegex(String firstname,String lastname);
}
varman
  • 8,704
  • 5
  • 19
  • 53