对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。

在查询中使用 string 函数

在本指南中,您可以学习如何使用 MongoDB Extension for Hibernate ORM 在 string 字段上调用 Hibernate 查询语言 (HQL) string 函数。string 函数允许您在查询中测量、重新格式化和组合 string 值。

您可以在 SELECT 子句中调用每个函数。Hibernate ORM 扩展将每个函数调用转换为 $project 阶段中的 MongoDB 聚合表达式。由于 Hibernate ORM 扩展不支持将函数调用作为计算表达式的操作数,因此您不能将 string 函数与算术操作符结合使用。

Hibernate ORM 扩展支持以下 string 函数:

function
MongoDB 翻译

length(), character_length() , char_length()

upper()

lower()

concat(), ||

substring()

locate()

replace()

trim()

$trim、$ltrim 或 $rtrim,具体取决于指定的修剪方向

pad(), lpad() , rpad()

由 $strLenCP、$substrCP 和 $concat 计算得出

repeat()

由 $range 和 $reduce 计算得出

注意

不支持的 string 函数

Hibernate ORM 扩展不支持 HQL position()、overlay()、left()、right() 和 collate() string 函数。调用其中一个函数的查询会抛出 FeatureNotSupportedException。

要使用不受支持的 string 函数,请调用 createNativeQuery() 方法并在您的 MongoDB 查询语言声明中包含相关操作符。要了解更多信息,请参阅执行原生数据库查询指南。

The examples in this guide use the Movie entity, which represents the sample_mflix.movies collection from the Atlas sample datasets. The Movie entity has the following definition:

import com.mongodb.hibernate.annotations.ObjectIdGenerator;import org.bson.types.ObjectId;import java.time.Instant;import java.util.List;import jakarta.persistence.Embedded; import jakarta.persistence.Entity;import jakarta.persistence.FetchType;import jakarta.persistence.Id;import jakarta.persistence.OneToMany;import jakarta.persistence.Table; @Entity@Table(name = "movies")public class Movie { @Id @ObjectIdGenerator private ObjectId id; private String title; private String plot; private int year; private List<String> cast; private List<String> directors; private Instant released; @Embedded private Awards awards; @OneToMany(mappedBy = "movie", fetch = FetchType.LAZY) private List<Comment> comments; public Movie(String title, String plot, int year, List<String> cast, List<String> directors, Instant released, Awards awards) { this.title = title; this.plot = plot; this.year = year; this.cast = cast; this.directors = directors; this.released = released; this.awards = awards; } public Movie() { } public ObjectId getId() { return id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getPlot() { return plot; } public void setPlot(String plot) { this.plot = plot; } public int getYear() { return year; } public void setYear(int year) { this.year = year; } public List<String> getCast() { return cast; } public void setCast(List<String> cast) { this.cast = cast; } public List<String> getDirectors() { return directors; } public void setDirectors(List<String> directors) { this.directors = directors; } public Awards getAwards() { return awards; } public void setAwards(Awards awards) { this.awards = awards; } public Instant getReleased() { return released; } public void setReleased(Instant released) { this.released = released; } public List<Comment> getComments() { return comments; } public void setComments(List<Comment> comments) { this.comments = comments; }}

学习如何创建使用 MongoDB Extension for Hibernate ORM 与此 MongoDB 示例集合交互的 Java 应用程序,请参阅 入门 教程。

本指南中的示例使用 Movie 实体的 title 字段,该字段存储 String 值。

length(str) 函数返回 string 中的字符数。您还可以将此函数调用为 character_length(str) 或 char_length(str),这些是同一函数的别名。由于 Hibernate ORM 扩展将对 $strLenCP 操作符的调用转换为,该函数计算 Unicode 代码点,而不是字节。

以下示例返回 sample_mflix.movies 集合中 "The Hunger Games" 电影的标题以及标题的长度:

var lengthResult = session.createQuery( "select title, length(title) as titleLength from Movie where title = :title", Object[].class) .setParameter("title", "The Hunger Games") .getResultList();for (var row : lengthResult) { System.out.println("Title: " + row[0] + ", Length: " + row[1]);}

Hibernate ORM 扩展将上述查询转换为下面的 $project 阶段:

{ "$project": { "title": true, "titleLength": { "$strLenCP": "$title" }, "_id": 0 }}

upper(str) 和 lower(str) 函数将 string 转换为大写或小写。

以下示例以大写形式返回 "The Hunger Games" 电影的标题:

var upperResult = session.createQuery( "select upper(title) as upperTitle from Movie where title = :title", String.class) .setParameter("title", "The Hunger Games") .getResultList();for (var upperTitle : upperResult) { System.out.println("Uppercase Title: " + upperTitle);}

Hibernate ORM 扩展将上述查询转换为下面的 $project 阶段:

{ "$project": { "upperTitle": { "$toUpper": "$title" }, "_id": 0 }}

concat(x, y) 函数将两个或多个值连接成一个字符串。Hibernate ORM 扩展在连接之前会将每个操作数转换为 string,因此您可以连接数值,或将数值连接到 string 值。

以下示例在 "The Hunger Games" 电影的标题后附加了一个感叹号:

var concatResult = session.createQuery( "select concat(title, '!') as excitedTitle from Movie where title = :title", String.class) .setParameter("title", "The Hunger Games") .getResultList();for (var excitedTitle : concatResult) { System.out.println("Concatenated Title: " + excitedTitle);}

提示

您还可以使用 double 管操作符 (||) 来连接两个值。以下查询与前面的示例等效:

"select title || '!' as excitedTitle from Movie where title = :title"

Hibernate ORM 扩展将上述查询转换为下面的 $project 阶段:

{ "$project": { "excitedTitle": { "$concat": [ { "$toString": "$title" }, { "$toString": "!" } ] }, "_id": 0 }}

substring(str, start) 函数返回 string 中从 start 位置开始的部分。start 位置是从 1 开始的,因此 string 中的第一个字符位于 1 位置。要将结果限制为固定数量的字符,请调用该函数为 substring(str, start, length)。

如果传递的 start 位置为 0 或负值,Hibernate ORM 扩展将从 string 开头开始读取。如果还传递了 length,扩展将从返回的长度中减去第一个位置之前的字符。示例,substring(str, -1, 3) 返回一个字符,substring(str, 0, 3) 返回两个字符。

以下示例返回 "The Hunger Games" 电影标题的前六个字符:

var substringResult = session.createQuery( "select substring(title, 1, 6) as titlePrefix from Movie where title = :title", String.class) .setParameter("title", "The Hunger Games") .getResultList();for (var titlePrefix : substringResult) { System.out.println("Title Prefix: " + titlePrefix);}

locate(pattern, str) 函数返回 pattern 在 string 中第一次出现的基于 1 的位置;如果 string 不包含该模式,则返回 0。

要从特定位置开始搜索,请调用函数 locate(pattern, str, start)。由于 start 位置是基于 1 的,因此 string 中的第一个字符位于位置 1。0 的 start 值或负数将从位置 1 开始搜索。

以下示例返回子字符串 "Hunger" 在 "The Hunger Games" 电影标题中的位置:

var locateResult = session.createQuery( "select locate('Hunger', title) as position from Movie where title = :title", Integer.class) .setParameter("title", "The Hunger Games") .getResultList();for (var position : locateResult) { System.out.println("Position: " + position);}

replace(str, pattern, replacement) 函数将 string 中所有出现的 pattern 替换为 replacement。Hibernate ORM 扩展将调用转换为 $replaceAll 操作符。所有三个参数都必须是字符串。

以下示例将 "The Hunger Games" 电影标题中的子字符串 "Hunger" 替换为 "Video":

var replaceResult = session.createQuery( "select replace(title, 'Hunger', 'Video') as newTitle from Movie where title = :title", String.class) .setParameter("title", "The Hunger Games") .getResultList();for (var newTitle : replaceResult) { System.out.println("New Title: " + newTitle);}

trim(str) 函数删除 string 开头和结尾的空格。要仅从 string 的一端删除字符,请使用 trim(leading char from str) 或 trim(trailing char from str)。要从 string 的两端删除空格以外的字符,请使用 both 和 from 关键字指定它,如 trim(both char from str) 所示。

以下示例将从 "The Hunger Games" 电影的标题中删除开头 "T" 字符:

var trimResult = session.createQuery( "select trim(leading 'T' from title) as trimmedTitle from Movie where title = :title", String.class) .setParameter("title", "The Hunger Games") .getResultList();for (var trimmedTitle : trimResult) { System.out.println("Trimmed Title: " + trimmedTitle);}

Hibernate ORM 扩展将上述查询转换为下面的 $project 阶段:

{ "$project": { "trimmedTitle": { "$ltrim": { "chars": "T", "input": "$title" } }, "_id": 0 }}

pad(str with length leading) 和 pad(str with length trailing) 函数通过在 string str 的开头或结尾添加空格,将 string 扩展到指定的 length。要使用空格以外的字符,请在方向后指定该字符,如 pad(str with 20 leading char) 所示。

您还可以调用 lpad(str, length, padding) 和 rpad(str, length, padding) 函数,它们使用可以包含多个字符的值来填充 string。Hibernate ORM 扩展会根据需要重复填充值,并将其截断以达到目标长度。示例,lpad('x', 6, 'ab') 返回 "ababax"。

在这些情况下,填充(Padding)的行为如下:

  • 如果目标长度短于 string,Hibernate ORM 扩展会通过删除目标长度之后的尾随字符将 string 截断为目标长度。

  • 如果目标长度为负数,则该函数返回一个空 string。

  • 如果填充值是空字符串,则该函数返回原始字符串。

以下示例使用前导星号将 "The Hunger Games" 电影的标题填充为 20 个字符:

var padResult = session.createQuery( "select pad(title with 20 leading '*') as paddedTitle from Movie where title = :title", String.class) .setParameter("title", "The Hunger Games") .getResultList();for (var paddedTitle : padResult) { System.out.println("Padded Title: " + paddedTitle);}

repeat(str, count) 函数返回一个包含 str 的 count 个副本的 string。

以下示例返回 "The Hunger Games" 电影的标题,重复两次:

var repeatResult = session.createQuery( "select repeat(title, 2) as repeatedTitle from Movie where title = :title", String.class) .setParameter("title", "The Hunger Games") .getResultList();for (var repeatedTitle : repeatResult) { System.out.println("Repeated Title: " + repeatedTitle);}

要了解有关创建查询筛选条件以及在查询声明中使用操作符的更多信息,请参阅指定查询指南。

要了解如何在查询中调用日期时间函数,请参阅 在查询中使用日期时间函数 指南。

要了解有关使用 HQL 和 JPQL 运行查询的更多信息,请参阅 Hibernate ORM 文档中的 Hibernate 查询语言指南。