Quantcast
Channel: How to query MongoDB with "like" - Stack Overflow
Viewing all articles
Browse latest Browse all 48

Answer by Priyanka Wagh for How to query MongoDB with "like"?

$
0
0

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.*/


Viewing all articles
Browse latest Browse all 48

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>