Spring MongoTemplate - find by regex in collection

∥☆過路亽.° 提交于 2019-12-23 01:41:18

问题


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

回答1:


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);


来源:https://stackoverflow.com/questions/35623672/spring-mongotemplate-find-by-regex-in-collection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!