欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

MongoDBSpringData创建索引工具

时间:2023-06-08
MongoDB Spring Data 创建索引工具 工具类

package com.man.tools.mongodb.factory;import cn.hutool.core.annotation.AnnotationUtil;import cn.hutool.json.JSONObject;import cn.hutool.json.JSONUtil;import com.theone.date.util.DateUtil;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.data.domain.Sort;import org.springframework.data.mongodb.core.MongoTemplate;import org.springframework.data.mongodb.core.index.CompoundIndex;import org.springframework.data.mongodb.core.index.CompoundIndexes;import org.springframework.data.mongodb.core.index.Index;import org.springframework.data.mongodb.core.index.IndexInfo;import java.util.Date;import java.util.HashSet;import java.util.Map;import java.util.Set;public class MongoJpaCollectionFactory { protected static final Logger LOGGER = LoggerFactory.getLogger(MongoJpaCollectionFactory.class); public static String getCollectionName(String docName, Date date) { return docName + DateUtil.format(date, DateUtil.yyyyMM); } public static void creatIndex(MongoTemplate mongoTemplate, String collectionName, Class<?> aClass) { Set indexSet = new HashSet<>(); for (IndexInfo indexInfo : mongoTemplate.indexOps(collectionName).getIndexInfo()) { indexSet.add(indexInfo.getName()); } CompoundIndex compoundIndex1 = AnnotationUtil.getAnnotation(aClass, CompoundIndex.class); if (compoundIndex1 != null) { index(collectionName, mongoTemplate, indexSet, compoundIndex1); return; } CompoundIndexes compoundIndexes = AnnotationUtil.getAnnotation(aClass, CompoundIndexes.class); for (CompoundIndex compoundIndex : compoundIndexes.value()) { index(collectionName, mongoTemplate, indexSet, compoundIndex); } } private static void index(String collectionName, MongoTemplate mongoTemplate, Set indexSet, CompoundIndex compoundIndex) { if (indexSet.contains(compoundIndex.name())) { return; } mongoTemplate.indexOps(collectionName).ensureIndex(getIndex(compoundIndex)); } private static Index getIndex(CompoundIndex compoundIndex) { String name = compoundIndex.name(); String def = compoundIndex.def(); Index index = new Index(); index.named(name); index.background(); JSONObject indexMap = JSONUtil.parseObj(def); for (Map.Entry keyOrder : indexMap.entrySet()) { String key = keyOrder.getKey(); String order = String.valueOf(keyOrder.getValue()); index.on(key, "1".equals(order) ? Sort.Direction.ASC : Sort.Direction.DESC); } return index; }}

使用 实例

package com.man.tools.mongodb;import org.springframework.data.annotation.Id;import org.springframework.data.mongodb.core.index.CompoundIndex;import org.springframework.data.mongodb.core.index.CompoundIndexes;import org.springframework.data.mongodb.core.mapping.document;@document(collection = "TestDoc")@CompoundIndexes({ @CompoundIndex(name = "age_-1_name_1", def = "{age:-1,name:1}", background = true)})public class TestDoc{ @Id private String id; private Integer age; private String name; public String getId() { return id; } public void setId(String id) { this.id = id; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; }}

测试

@RunWith(SpringJUnit4ClassRunner.class)//启动Spring@SpringBootTest(classes = Application.class)public class baseTest {@Resource private MongoTemplate mongoTemplate;@Testpublic void testIndexOp() { // 按年分表ArrayList> list = CollectionUtil.newArrayList(TestDoc.class);for (Class<?> aClass : list) {String collectionNamebase = mongoTemplate.getCollectionName(aClass);String collectionName =collectionNamebase + "2020";if (mongoTemplate.collectionExists(collectionName)) {MongoJpaCollectionFactory.creatIndex(mongoTemplate, collectionName, aClass);}}}}

别问为什么MongoDB还要手动去分表,因为贫穷搭不起集群!!!

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。