IndexOperations 接口定义了一个可以返回 IndexInfo 对象列表的 getIndexInfo() 方法。此 IndexInfo 对象列表包含在集合上定义的所有索引。
下面示例演示在 name 属性上定义一个索引,然后获取集合所有索引信息,关键代码如下:
// 创建索引 Index index = new Index().on("name", Sort.Direction.ASC); String val = mongoTemplate.indexOps(Person.class).ensureIndex(index); // 获取索引索引 List<IndexInfo> indexInfoList = mongoTemplate.indexOps(Person.class).getIndexInfo(); for(IndexInfo indexInfo : indexInfoList) { System.out.println(indexInfo); }
(1)application.properties 配置文件
# Log logging.level.root=debug # MongoDB spring.data.mongodb.uri=mongodb://localhost:27017/test
(2)AppConfig.java 配置类,配置 MongoTemplate 对象
import lombok.extern.slf4j.Slf4j; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.mongodb.MongoDatabaseFactory; import org.springframework.data.mongodb.core.MongoTemplate; /** * 配置 MongoTemplate * @author hxstrive.com 2022/12/23 */ @Slf4j @Configuration public class AppConfig { @Bean public MongoTemplate mongoTemplate(MongoDatabaseFactory mongoDatabaseFactory) { log.info("mongoTemplate({}, {})", mongoDatabaseFactory); return new MongoTemplate(mongoDatabaseFactory); } }
(3)集合 person 对应的实体
import lombok.Builder; import lombok.Data; import lombok.ToString; @Data @ToString @Builder public class Person { /** 用户ID */ private int id; /** 用户姓名 */ private String name; /** 年龄 */ private int age; }
(4)客户端代码,说明见注释。
import com.hxstrive.springdata.mongodb.entity.Person; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.domain.Sort; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.index.Index; import org.springframework.data.mongodb.core.index.IndexInfo; import java.util.List; /** * 获取索引信息示例 * @author hxstrive.com */ @SpringBootTest public class IndexManagementDemo2 { @Autowired private MongoTemplate mongoTemplate; @BeforeEach public void init() { mongoTemplate.dropCollection(Person.class); // 准备数据 mongoTemplate.insert(Person.builder().id(100).name("Tom").age(27).build()); mongoTemplate.insert(Person.builder().id(200).name("Helen").age(30).build()); mongoTemplate.insert(Person.builder().id(300).name("Bill").age(47).build()); mongoTemplate.insert(Person.builder().id(400).name("Joe").age(20).build()); } @Test public void contextLoads() { Index index = new Index().on("name", Sort.Direction.ASC); String val = mongoTemplate.indexOps(Person.class).ensureIndex(index); System.out.println("val=" + val); // 结果: // val=name_1 List<IndexInfo> indexInfoList = mongoTemplate.indexOps(Person.class).getIndexInfo(); for(IndexInfo indexInfo : indexInfoList) { System.out.println(indexInfo); } // 结果: // IndexInfo [indexFields=[IndexField [ key: _id, direction: ASC, type: DEFAULT, weight: NaN]], // name=_id_, unique=false, sparse=false, language=, partialFilterExpression=null, collation=null, // expireAfterSeconds=null] // IndexInfo [indexFields=[IndexField [ key: name, direction: ASC, type: DEFAULT, weight: NaN]], // name=name_1, unique=false, sparse=false, language=, partialFilterExpression=null, collation=null, // expireAfterSeconds=null] } }