栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

MongoDB Spring Data创建索引工具

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

MongoDB Spring Data创建索引工具

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;
	@Test
	public 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还要手动去分表,因为贫穷搭不起集群!!!
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/737256.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号