mongoDB导出数据库所有集合内容到json文件

网上搜了一圈,官方并有提供批量导出所有集合到json文件的方法。有不少脚本可以实现,但是我还是习惯用java,如下

package starcLL.webClient;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.bson.Document;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mongodb.BasicDBObject;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.MongoException;
import com.mongodb.ServerAddress;
import com.mongodb.client.ListIndexesIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;

@Component
public class MongodbClient {

    private static String starcLL_mongodb_ip="填写你的ip";
    private static int starcLL_mongodb_port=填写你的端口;
    private static String starcLL_mongodb_name="填写你的数据库名"; 

    public static MongoClient mongoClient = null;
    public static MongoDatabase database = null;
    public static ServerAddress serverAddress = null;
    public static MongoCredential credentials = null;
    public static List<ServerAddress> addressLists = new ArrayList<ServerAddress>();
    public static List<MongoCredential> credentialsLists = new ArrayList<MongoCredential>();

    public static MongoClient getMongoClient() {
        if (null == mongoClient) {
            try {
                serverAddress = new ServerAddress(starcLL_mongodb_ip, starcLL_mongodb_port);
                addressLists.add(serverAddress);
                credentials = MongoCredential.createCredential("数据库", "用户名","密码".toCharArray());
                credentialsLists.add(credentials);
                return new MongoClient(addressLists,credentialsLists);
            } catch (MongoException e) {
                System.out.println(e.toString());
            }
        }
        return mongoClient;
    }



    public static void main(String[] args) throws IOException {
        MongoClient mongoClient=getMongoClient();
        Set<String> collectionNames=mongoClient.getDB(starcLL_mongodb_name).getCollectionNames();
        MongoDatabase mongoDatabase=mongoClient.getDatabase(starcLL_mongodb_name);
        System.out.println("开始...");

        int i=collectionNames.size();
        for (String cName:collectionNames) {
            JSONArray jsonArray=new JSONArray();
               System.out.println("获取集合:"+cName);
               MongoCursor<Document> cursor = mongoDatabase.getCollection(cName).find().iterator();
            try {
                while (cursor.hasNext()) {
                    jsonArray.add(cursor.next().toJson());
                }
            } finally {
                cursor.close();
            }
            File txt=new File("C:/Users/Administrator/Desktop/svc/"+i+".json");
            if(!txt.exists()){
               txt.createNewFile();
            }
            byte bytes[]=new byte[512];
            bytes=jsonArray.toString().getBytes();    
            int b=jsonArray.toString().length();    
            FileOutputStream fos=new FileOutputStream(txt);
            fos.write(bytes,0,b);
            fos.close();
            i--;
        }

    }
}
上一篇:数据湖的概念


下一篇:CYQ.DBImport 数据库反向工程及批量导数据库工具 V2.0 发布[增加批量导出数据库脚本及数据库设计文档]