自动创建索引
使用注解的形式,自动创建索引,帮助你省去索引维护的相关操作,可以将主要精力放在查询上
所在数据库和集合名
- 使用自动创建索引功能的实体类上,需要使用
@CollectionName注解标注,不然会扫描不到 - 此篇文档中的索引注解,均可与
@CollectionName注解互动,使用该注解配置的集合名称和数据库属性
配置
- 使用MongoPlus的自动创建索引功能,需开启自动创建索引配置。
- 多模块项目,包名不同,则需要在主程序配置文件中,指定存在索引类的子包路径
yaml
mongo-plus:
configuration:
auto-create-index: true #开启自动创建索引功能
auto-scan-packages: # 多模块且包名不同,需要指定子包的包路径
- com.xxxx.entity
- com.xxxx.entity1注解
@MongoIndex
- 注解式索引自动创建
- 通过该注解,可以对MongoDB指定字段创建索引
| 属性 | 类型 | 必须指定 | 默认值 | 描述 |
|---|---|---|---|---|
| name | String | 否 | 字段名 | 索引名称 |
| unique | boolean | 否 | false | 唯一索引 |
| direction | IndexDirection | 否 | IndexDirection.ASC | 索引排序方向 |
| sparse | boolean | 否 | false | 稀疏索引 |
| expireAfterSeconds | long | 否 | -1 | 索引过期时间,以秒为单位 |
| expireAfter | String | 否 | "" | 自定义类型的索引过期时间 |
| partialFilterExpression | String | 否 | "" | 部分索引,例:{"$gt",5} |
| background | boolean | 否 | false | 是否应该在后台创建索引 |
expireAfter属性,自定义类型的索引过期时间表示:
- 天:
@MongoIndex(expireAfter = "1d") - 时:
@MongoIndex(expireAfter = "3h") - 分:
@MongoIndex(expireAfter = "30m") - 秒:
@MongoIndex(expireAfter = "30s")
示例
java
@CollectionName("user")
public class User{
//设置为唯一索引
@MongoIndex(unique=true)
private String userName;
}@MongoTextIndex
- 注解式文本索引自动创建
- 通过该注解,可以对MongoDB指定字段创建为文本索引
- 注意文本索引限制
| 属性 | 类型 | 必须指定 | 默认值 | 描述 |
|---|---|---|---|---|
| fields | String[] | 是 | 索引字段,传入多个值即为复合文本索引 | |
| name | String | 否 | 字段名 | 索引名称 |
| language | TextLanguages | 否 | TextLanguages.ENGLISH | 文本语言,默认英语 |
| textIndexVersion | long | 否 | -1 | 尽可能始终使用默认索引版本。 仅在出于兼容性原因需要时才覆盖默认版本 |
示例
java
@CollectionName("user")
public class User {
}@MongoHashIndex
- 注解式哈希索引自动创建
- 通过该注解,可以对MongoDB指定字段创建为哈希索引
示例
java
@CollectionName("user")
public class User {
@MongoHashIndex
private String userName;
}@MongoCompoundIndex
- 注解式复合索引自动创建
- 通过该注解,可以对MongoDB指定字段创建复合索引
- 复合索引注解,应标注在类上,或可以
@MongoCompoundIndexes注解的内部属性使用
| 属性 | 类型 | 必须指定 | 默认值 | 描述 |
|---|---|---|---|---|
| value | String | 是 | 复合索引值,这里需要传入json格式 | |
| name | String | 否 | 字段名 | 索引名称 |
| unique | boolean | 否 | false | 唯一复合索引 |
| sparse | boolean | 否 | false | 稀疏复合索引 |
| partialFilterExpression | String | 否 | "" | 部分索引 |
| background | boolean | 否 | false | 是否应该在后台创建索引 |
value属性,需要传入json格式数据:
- 如:
@CompoundIndex("{'field1':1,'field2':-1},-1表示倒序,1表示正序 - 当然MongoPlus也支持使用
$来表示该字段需要引用类中字段的名称(@CollectionFieldz注解可控制),如下: @CompoundIndex("{'$field1':1,'$field2':-1},该操作将会在本类中找到field1字段,然后获取@CollectionField注解,使用注解的value作为key,如果没有注解则会去掉$当做key
partialFilterExpression属性,和@MongoIndex的partialFilterExpression并不同,该属性需手动指定字段
- 例:
{"field1" : {"$gt",5}} - 同样,
partialFilterExpression属性也可以使用$来表示字段需要引用类中的字段,如:{"$field1" : {"$gt",5}}
示例
java
@CollectionName("user")
@MongoCompoundIndex(
value = "{'userName':1,'$userAge':-1}"
unique = true
partialFilterExpression = "{\"$userAge\":{\"$gt\",5}}"
)
public class User {
public String userName;
@CollectionField("user_age")
private Integer userAge;
}@MongoCompoundIndexs
- 多个复合索引
- 使用该注解,可以将多个复合索引注解整合到一起使用
| 属性 | 类型 | 必须指定 | 默认值 | 描述 |
|---|---|---|---|---|
| value | MongoCompoundIndex[] | 是 | 多个复合索引 |
示例
java
@CollectionName("user")
@MongoCompoundIndexes({
@MongoCompoundIndex(value = "{'$userName':1,'userAge':1}"),
@MongoCompoundIndex(value = "{'$userStatus':1,'userAddress':1}")
})
public class User {
public String userName;
@CollectionField("user_age")
private Integer userAge;
private String userAddress;
private Integer userAge;
@MongoHashIndex
private Integer userStatus;
}@MongoGeoIndex
- 自动创建2dsphere索引或2d索引
- 需指定需要创建哪种索引
| 属性 | 类型 | 必须指定 | 默认值 | 描述 |
|---|---|---|---|---|
| type | GeoType | 是 | 索引类型 |
示例
java
@CollectionName("user")
public class User {
// 指定创建2dsphere索引
@MongoGeoIndex(type = GeoType.SPHERE)
private Geometry location;
}@MongoIndexDs
- 自动创建索引的数据源
- 用来标识自动创建的索引应该在哪个数据源
- 默认为配置中的主数据源,非必要无需使用此配置
| 属性 | 类型 | 必须指定 | 默认值 | 描述 |
|---|---|---|---|---|
| dataSource | String | 否 | 数据源名称 |

