//package com.ycl.config; // //import com.mongodb.client.ListIndexesIterable; //import com.mongodb.client.MongoCollection; //import org.bson.Document; //import org.springframework.boot.context.event.ApplicationReadyEvent; //import org.springframework.context.event.EventListener; //import org.springframework.data.mongodb.core.MongoTemplate; //import org.springframework.stereotype.Component; // //import java.util.concurrent.atomic.AtomicReference; // ///** // * @author:xp // * @date:2024/8/2 13:54 // */ //@Component //public class MongoInit { // // /** // * springboot启动时创建mongo的索引 // * // * @param testEvent // */ // @EventListener(classes = {ApplicationReadyEvent.class}) // public void createIndex(ApplicationReadyEvent testEvent) { // MongoTemplate mongo = testEvent.getApplicationContext().getBean(MongoTemplate.class); // MongoCollection coll = mongo.getCollection("aa"); // // // } // // /** // * 检查没有索引就创建 // * // * @param mongo mongodb实例 // * @param collectionName mongodb集合名称 // */ // private void exitAndCreate(MongoTemplate mongo, String collectionName) { // // 获取集合 // MongoCollection collection = mongo.getCollection("yourCollectionName"); // // // 创建一个全文索引,这里索引所有字符串字段 // Document createIndex = new Document("$**", "text"); // // 设置语言 // createIndex.append("default_language", "chinese"); // ListIndexesIterable indexes = collection.listIndexes(); // AtomicReference exit = new AtomicReference<>(Boolean.FALSE); // indexes.forEach(index -> { // if (createIndex.equals(index)) { // exit.set(Boolean.TRUE); // } // }); // if (! exit.get()) { // collection.createIndex(createIndex); // } // } // //}