3* 升级到5.* ,会碰到索引不兼容问题
use stars_sales
var collections = db.getCollectionNames();
collections.forEach(function(collection) {
var indexes = db[collection].getIndexes();
print("Indexes for collection " + collection + ":");
printjson(indexes);
});
解析索引
package local.local_test;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.toolkit.StringUtils;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class Test2 {
public static void main(String[] args) {
//test();
testNew();
}
private static void test() {
List<String> fileLine = ReadFileLine.getFileLine("D://3.txt");
StringBuffer sb=new StringBuffer();
for(String idx:fileLine) {
if(idx.indexOf("Indexes for collection")!=-1) {
sb.append(",");
}else {
sb.append(idx);
}
}
String join = "["+sb.toString()+"]";
join=join.replace(" ", "");
System.out.println(join+"/n");
JSONArray parseArray = JSON.parseArray(join);
Object object = parseArray.get(0);
System.out.println("开始/n");
for(Object item:parseArray) {
JSONArray g=(JSONArray)item;
for(Object gitem:g) {
JSONObject b=(JSONObject)gitem;
String idxResult=b.get("key").toString();
JSONObject ext=new JSONObject();
ext.put("name", b.getString("name"));
ext.put("background", true);
if(b.get("unique")!=null) {
ext.put("unique", true);
}
idxResult=idxResult+","+ext.toJSONString();
String col=b.getString("ns").substring(b.getString("ns").indexOf(".")+1);
System.out.println(b.getString("ns")+"\t"+col+"\t"+b.getString("name")+"\t"+"db."+col+".createIndex("+idxResult+")");
}
}
}
private static void testNew() {
List<String> fileLine = ReadFileLine.getFileLine("D://2.txt");
String join2 = String.join("", fileLine);
String[] split = join2.split("Indexes for collection ");
Map<String,String> result=Maps.newHashMap();
for(int i=0;i<split.length;i++) {
String string = split[i];
//System.out.println(string);
if(StringUtils.isEmpty(string)) {
continue;
}
result.put(string.substring(0,string.indexOf(":")), string.substring(string.indexOf(":")+1));
}
for(Entry<String, String> item:result.entrySet()) {
String key = item.getKey();
String value = item.getValue();
for(Object gitem:JSON.parseArray(value)) {
JSONObject b=(JSONObject)gitem;
System.out.println(key+"\t"+b.getString("name")+"\t"+b);
}
}
}
}