I'm trying to find recipes in a database where their titles contain the string in the method parameter. I don't want it to have to be an exact match (the way it is now) but instead if the string exists anywhere in the 'title' field of the recipe it should be considered a match. This means if I'm searching for "beef" you could expect to get a lot of recipes. Here's my code:
import java.util.List;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.Query;
public interface RecipeRepository extends MongoRepository<Recipe, String> {
public Recipe findByTitle(String title);
//@Query("{ 'title': /.*?0.*/ }")
@Query("{ 'title' : ?0 }")
public List<Recipe> findByTitleLike(String title);
}
The code currently works with the ?0
placeholder and finds matches that match the title exactly. You can see my commented out line above with a regular expression, where I was trying to match basically anything on either side of the title
parameter, so basically if it exists in the title at all it should match.
I followed this answer to construct the regular expression query: https://stackoverflow.com/a/3305687/8887398
I've also looked at the docs: https://docs.mongodb.com/manual/reference/operator/query/regex/
I keep getting this error when I run my code:
Unsatisfied dependency expressed through field 'repository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'recipeRepository': Invocation of init method failed; nested exception is com.mongodb.util.JSONParseException:
{ 'title': /.*"_param_0".*/ }
There's a little arrow pointing to a specific spot in the string, not sure if relevant:
I'm really not sure what I'm doing wrong. Can anyone see my mistake? The error message isn't really helping me either.