Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MongoTemplate - find by regex in collection

Saying i have a multivalue field in a mongo document:

public class MongoEntity{
    private List<String> field;
}

What would be the correct criteria for querying a regex in that field?

I've tried

Criteria.where(searchText).regex(searchText).in("field");

But that results in

org.springframework.data.mongodb.UncategorizedMongoDbException: 
Can't canonicalize query: BadValue unknown top level operator: $in; 
nested exception is com.mongodb.MongoException: Can't canonicalize query: BadValue unknown top level operator: $in
like image 751
Daniel Avatar asked Nov 22 '25 17:11

Daniel


1 Answers

So after many trial and errors, turns out it's simpler (although a little counter-intuitive in my opinion) than it thought:

Criteria.where("field").regex(searchText);
like image 102
Daniel Avatar answered Nov 25 '25 07:11

Daniel