Spring MongoTemplate-通过正则表达式在集合中查找 [英] Spring MongoTemplate - find by regex in collection

查看:708
本文介绍了Spring MongoTemplate-通过正则表达式在集合中查找的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我在mongo文档中有一个多值字段:

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?

我尝试过

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

但这会导致

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

推荐答案

因此,在经过多次尝试和错误之后,事实证明,它比想像的要简单(尽管有点反直觉):

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

这篇关于Spring MongoTemplate-通过正则表达式在集合中查找的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆