对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
See how MongoDB 9.0 delivers up to 2x higher throughput.
MongoDB Branding Shape
Register now >
Docs 菜单

用于查询优化的索引

在本指南中,您可以学习;了解如何使用MongoDB Extension for Hibernate ORM 在实体上声明单字段、复合和唯一索引。当您启用模式生成时,Hibernate ORM 扩展会将您在实体上声明的索引和唯一约束转换为MongoDB索引操作。

要创建在实体上声明的索引和唯一约束,请在应用程序的配置中启用模式生成。您可以使用 create 或 create-drop动作模式启用:

  • create:当 SessionFactory 关闭时,创建模式对象而不删除它们。

  • create-drop: Creates the schema objects when the SessionFactory opens and drops them when it closes.

警告

create-drop动作用于开发和测试。当需要保留生产数据时,请勿使用它。

如果使用 Spring Boot,请将以下属性添加到 application.properties文件中:

spring.jpa.properties.jakarta.persistence.schema-generation.database.action=create-drop

如果直接配置 Jakarta Persistence,请将以下属性添加到 persistence.xml文件中:

<property name="jakarta.persistence.schema-generation.database.action"
value="create-drop"/>

或者,您可以在 hibernate.properties文件中设立该属性:

jakarta.persistence.schema-generation.database.action=create-drop

当模式生成运行时,Hibernate ORM 扩展会创建集合,并为实体声明的索引和唯一约束发出MongoDB createIndexes 命令。

本指南中的示例通过将 @Index 和 @UniqueConstraint 注释添加到以下 Movie 实体的 @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;
public Movie(String title, String plot, int year, List<String> cast, List<String> directors) {
this.title = title;
this.plot = plot;
this.year = year;
this.cast = cast;
this.directors = directors;
}
public Movie() {
}
// Getter and setter methods
}

单字段索引是指引用集合文档中的单个字段的索引。它们提高了单个字段的查询和排序性能。

以下示例按降序在 year字段上创建单个字段索引:

@Table(
name = "movies",
indexes = {
@Index(name = "idx_year", columnList = "year desc")
})

将 columnList 设置为字段名称,并添加 asc 或 desc 以指定方向。如果省略方向, MongoDB将创建升序索引。 name 元素指定MongoDB索引的名称。

有关更多信息,请参阅 MongoDB Server 手册中的单字段索引。

复合索引包含对集合文档中多个字段的引用,从而提高查询和排序性能。

以下示例按降序对 title 和 year 字段创建复合索引:

@Table(
name = "movies",
indexes = {
@Index(name = "idx_title_year", columnList = "title desc,year desc")
})

columnList 中字段的顺序决定了MongoDB对索引键的排序顺序,从而影响索引可以支持的查询前缀。示例,title 上的索引(后跟 year)可以支持仅匹配 title 的查询,而 year 上的索引不能帮助仅匹配 title 的查询。

有关更多信息,请参阅 MongoDB Server 手册中的复合索引。

唯一索引可确保索引字段不存储重复值。

您可以使用 @Index 或 @UniqueConstraint 注解创建唯一索引:

  • 需要指定字段方向等索引选项时,请使用 @Index(unique = true)。

  • 如果要使用表级约束模型,请使用 @UniqueConstraint。 columnNames 中的字段定义键顺序,@UniqueConstraint 不指定字段方向,因此生成的唯一索引为升序。

以下示例在 title字段上创建唯一索引:

@Table(
name = "movies",
indexes = {
@Index(name = "idx_title", columnList = "title", unique = true)
})

以下示例对 title 和 plot 字段创建唯一性约束:

@Table(
name = "movies",
uniqueConstraints = {
@UniqueConstraint(name = "uk_title_plot", columnNames = {"title", "plot"})
})

MongoDB将索引字段的 null 或缺失值视为索引值。由于唯一性约束,单字段唯一索引只能包含一个在其索引项中具有 null 值的文档。

重要

创建唯一索引可能会失败

如果集合已包含具有重复字段值的文档,则MongoDB无法对指定字段创建唯一索引。在创建唯一索引之前,请检查现有数据中是否存在重复值。

有关更多信息,请参阅MongoDB Server手册中的“唯一索引”。

要学习;了解有关创建实体和映射嵌入式数据的更多信息,请参阅创建实体以表示集合指南。

要学习;了解如何使用实体运行数据库操作,请参阅执行增删改查操作和指定查询指南。

要查看 Hibernate ORM 扩展支持的索引类型列表,请参阅功能兼容性页面。

要学习;了解有关本指南中使用的 Jakarta Persistence 注解的更多信息,请参阅 Jakarta Persistence API文档。