Above answers are perfectly answering the questions about core mongodb query. But when using pattern based search query such as:
{"keywords":{ "$regex": "^toron.*"}}
or
{"keywords":{ "$regex": "^toron"}}
in spring boot jpa repository query with @Query annotation, use query something like:
@Query(value = "{ keyword : { $regex : ?0 } }")List<SomeResponse> findByKeywordContainingRegex(String keyword);
and call should be either of:
List<SomeResponse> someResponseList = someRepository.findByKeywordsContainingRegex("^toron");List<SomeResponse> someResponseList = someRepository.findByKeywordsContainingRegex("^toron.*");
But Never use:
List<SomeResponse> someResponseList = someRepository.findByKeywordsContainingRegex("/^toron/");List<SomeResponse> someResponseList =someRepository.findByKeywordsContainingRegex("/^toron.*/");
Important point to note: each time ?0 field in @Query statement is replaced with double quoted string. so forwardslash(/)should not be used in these cases! always go for pattern using double quotes in searching pattern!! eg. use "^toron" or "^toron.*"
over /^toron/ or /^toron.*/