Spring boot 在springboot应用程序中使用mongotemplate实现Like运算符

Spring boot 在springboot应用程序中使用mongotemplate实现Like运算符,spring-boot,mongotemplate,Spring Boot,Mongotemplate,在使用mongotemplate的spring Boot应用程序中,我需要编写一个查询,以使用LIKE操作符获得结果。但是在mongotemplate中,就像在SQL中一样,我没有可以使用的直接查询 我尝试了一些选项,如Aggregate和regex方法,但没有成功 在MongoDB也尝试了同样的方法 db.getCollection('Months').find({"name":/Jan/}). 因此,在月份表中,我必须检索“name”列中包含“Jan”字母的结果。但是在运行这个查询时,假设

在使用mongotemplate的spring Boot应用程序中,我需要编写一个查询,以使用
LIKE
操作符获得结果。但是在mongotemplate中,就像在SQL中一样,我没有可以使用的直接查询

我尝试了一些选项,如
Aggregate
regex
方法,但没有成功

在MongoDB也尝试了同样的方法

db.getCollection('Months').find({"name":/Jan/}).
因此,在
月份
中,我必须检索“name”列中包含“Jan”字母的结果。但是在运行这个查询时,假设我有一个名为'DecJan'的条目,在这种情况下,我无法检索它

Query constantQuery = new Query();``
constantQuery.addCriteria(Criteria.where("name").regex("DecJan", "i"));

List<Months> pp = this.mongoTemplate.find(constantQuery, Months.class);
queryconstantquery=newquery()``
constantQuery.addCriteria(Criteria.where(“name”).regex(“DecJan”,“i”));
List pp=this.mongoTemplate.find(constantQuery,Months.class);
所以,预期的结果是,如果我在一个表中有两个条目,比如“一月”,“十二月”
在查询中,如果我通过“Jan”,我需要使用mongotemplate检索两个结果。

更新的复制链接不引用所需的mongotemplate实例

顺便说一下,我使用aggregationOperation进行了同样的操作,并得到了结果

  • 创建聚合操作
  • 生成具有所需匹配项的Aggregation对象
  • 使用Mongotemplate.aggregate进行查询(AggregationOperation、Aggregation)
  • 示例代码: AggregationOperation aggregate=Aggregation.project()。和(“FirstName”).as(“name”)

    现在可以检索结果。

    可能重复的
            AggregationOperation operation = Aggregation.match(Criteria.where("name").regex(pattern, "i"));
    
            Aggregation aggregation = Aggregation.newAggregation(aggregate, operation);