Overview
在本指南中,您可以学习;了解如何使用MongoDB Extension for Hibernate ORM 在MongoDB 数据库上运行原生查询。原生查询允许您使用MongoDB查询语言 (MQL) 来指定查询,而不是使用 Hibernate Query Language (HQL) 或 Jakarta Persistence Query Language (JPQL)。 MQL是一种查询语法,旨在与 MongoDB 基于文档的模型进行交互。
Hibernate ORM 的 createQuery() 方法不支持某些MongoDB查询功能。 createNativeQuery() 方法允许您在MQL中指定数据库查询并绕过 Hibernate ORM 扩展的某些操作限制。
您还可以直接对 MongoClient对象运行查询,以扩展查询功能。
样本数据
本指南中的示例使用Movie 实体,它表示Atlas示例数据集中的sample_mflix.movies 集合。Movie 实体具有以下定义:
import com.mongodb.hibernate.annotations.ObjectIdGenerator; import org.bson.types.ObjectId; import jakarta.persistence.Entity; import jakarta.persistence.Id; import jakarta.persistence.Table; public class Movie { private ObjectId id; private String title; private String plot; private int year; private int runtime; private String[] cast; public Movie(String title, String plot, int year, int runtime, String[] cast) { this.title = title; this.plot = plot; this.year = year; this.runtime = runtime; this.cast = cast; } 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 int getRuntime() { return runtime; } public void setRuntime(int runtime) { this.runtime = runtime; } public String[] getCast() { return cast; } public void setCast(String[] cast) { this.cast = cast; } }
要学习;了解如何创建Java应用程序以使用MongoDB Extension for Hibernate ORM 与此MongoDB示例集合交互,请参阅 入门教程。
运行原生查询
要运行原生MongoDB查询,请指定MongoDB查询语言 (MQL)声明,其中包括要查询的集合以及聚合管道中的查询条件。
MQL语句采用以下格式:
String mqlSyntax = """ { aggregate: "<collection to query>", pipeline: [ <aggregation pipeline stages> ] } """;
然后,将MQL声明传递给 createNativeQuery() 方法。您无法运行使用参数值的原生查询。相反,请在MQL声明中指定搜索词。
您可以使用原生查询来执行以下操作:
对文档字段进行筛选和排序
此示例通过将MQL声明传递给 createNativeQuery() 方法来对 sample_mflix.movies集合运行原生查询。代码指定了以下聚合管道阶段:
$match:筛选标题字段值为以下内容的文档:"The Parent Trap"$sort:按匹配文档的year字段降序排序$project:返回Movie实体中定义的每个文档字段
String nativeQuery = """ { aggregate: "movies", pipeline: [ { $match: { title: { $eq: "The Parent Trap" } } }, { $sort: { year: -1 } }, { $project: { title: 1, plot: 1, year: 1, runtime: 1, cast: 1 } } ] } """; var results = session.createNativeQuery(nativeQuery, Movie.class) .getResultList(); for (Movie movie : results) { System.out.println("Title: " + movie.getTitle() + ", Year: " + movie.getYear()); }
Title: The Parent Trap, Year: 1998 Title: The Parent Trap, Year: 1961
使用算术操作符
Hibernate ORM 扩展当前不支持使用算术运算符的 HQL 或 JPQL 语句。但是,您可以在MQL声明中使用 MongoDB 的算术运算符对数据执行算术运算。
以下示例对 sample_mflix.movies集合运行原生查询,以执行以下操作:
指定一个
$match阶段,以匹配year值大于2000且runtime字段存在的文档指定
$addFields阶段以添加名为runtimeHours的新字段对于新的
runtimeHours字段,使用$divide算术运算操作符将runtime值从分钟转换为小时指定一个
$project阶段以返回Movie实体中定义的每个文档字段,但主键字段除外打印每个已更新文档的
title值
String nativeQuery = """ { aggregate: "movies", pipeline: [ { $match: { year: { $gt: 2000 }, runtime: { $exists: true } } }, { $addFields: { runtimeHours: { $divide: [ "$runtime", 60 ] } }}, { $project: { title: 1, plot: 1, year: 1, runtime: 1, cast: 1 }} ] } """; var results = session.createNativeQuery(nativeQuery, Movie.class) .getResultList(); for (Movie result : results) { System.out.println("Added field to movie: " + result.getTitle()); }
Added field to movie: Kate & Leopold Added field to movie: Crime and Punishment Added field to movie: Glitter Added field to movie: The Manson Family Added field to movie: The Dancer Upstairs Added field to movie: Fantastic Four ...
运行MongoDB搜索查询
您可以运行原生查询来对数据库执行MongoDB Search 查询,这是对数据的细粒度文本搜索。这些查询提供高级搜索功能,例如匹配文本短语、对结果的相关性进行评分以及突出显示匹配项。
重要
您无法在ACID 事务中运行MongoDB Search查询。
要指定搜索查询,请创建涵盖要查询的字段的搜索索引。然后,将聚合管道传递给包含 $search 或 $searchMeta 阶段的 createNativeQuery() 方法。
此示例通过将 $search管道阶段传递给 createNativeQuery() 方法来运行搜索查询。该代码执行以下操作:
指定涵盖
plot字段的搜索索引。确保将<indexName>占位符替换为您的搜索索引名称。查询
plot值包含字符串"whirlwind romance"且字符串之间的单词数不超过3个的文档指定一个
$project阶段以返回Movie实体中定义的每个文档字段,但主键字段除外打印匹配文档的
title和plot值
String nativeQuery = """ { aggregate: "movies", pipeline: [ { $search: { index: "<indexName>", phrase: { path: "plot", query: "whirlwind romance", slop: 3 } } }, { $project: { title: 1, plot: 1, year: 1, runtime: 1, cast: 1 } } ] } """; var results = session.createNativeQuery(nativeQuery, Movie.class) .getResultList(); for (Movie result : results) { System.out.println("Title: " + result.getTitle() + ", Plot: " + result.getPlot()); }
Title: Tokyo Fiancèe, Plot: A young Japanophile Belgian woman in Tokyo falls into a whirlwind romance with a Francophile Japanese student. Title: Designing Woman, Plot: A sportswriter and a fashion-designer marry after a whirlwind romance, and discover they have little in common. Title: Vivacious Lady, Plot: On a quick trip to the city, young university professor Peter Morgan falls in love with nightclub performer Francey Brent and marries her after a whirlwind romance. But when he goes back ... Title: Ek Hasina Thi, Plot: A woman falls for a charming and mysterious businessman. The whirlwind romance turns sour when she is framed for his underworld crimes. Now, finally out of prison she is ready for sweet revenge. Title: Kick, Plot: An adrenaline junkie walks away from a whirlwind romance and embraces a new life as a thief, though he soon finds himself pursued by veteran police officer and engaged in a turf war with a local gangster. Title: A Tale of Winter, Plot: Felicie and Charles have a serious if whirlwind holiday romance. Due to a mix-up on addresses they lose contact, and five years later at Christmas-time Felicie is living with her mother in ...
运行 MongoClient 操作
如果要运行createQuery() 方法和 createNativeQuery() 方法都不支持的数据库操作,可以直接在Java应用程序中操作 MongoClient对象。使用 MongoClient 时,您可以访问权限MongoDB Java同步驱动程序的功能。
要学习;了解如何使用Java驾驶员与MongoDB交互,请参阅MongoDB Java驱动程序文档。
使用 MongoClient 创建索引
您无法使用 Hibernate ORM 扩展在集合上创建索引,但可以实例化 MongoClient 并使用Java驱动程序的 createIndex() 方法。以下代码在 sample_mflix.movies集合上创建 title字段索引:
// Replace the <connection URI> placeholder with your MongoDB connection URI String uri = "<connection URI>"; MongoClient mongoClient = MongoClients.create(uri); MongoDatabase db = mongoClient.getDatabase("sample_mflix"); MongoCollection<Document> collection = db.getCollection("movies"); String indexResult = collection.createIndex(Indexes.ascending("title")); System.out.println(String.format("Index created: %s", indexResult));
Index created: title_1
更多信息
要进一步学习;了解本指南中讨论的查询语言,请参阅以下资源:
Hibernate ORM 文档中的 Hibernate 查询语言指南