网上搜了一圈,官方并有提供批量导出所有集合到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--;}}
}

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

  1. MongoDB基础--数据库和集合基本操作

        本笔记针对https://www.shiyanlou.com/courses/running/50 网站对mongoDB的学习和总结. 1,启动mongoDB     因为mongoDB服务并 ...

  2. MongoDB新建数据库、集合以及用户创建和权限设置

    有段时间没用MongoDB了,最近因为业务需要开始使用MongoDB.这里就简单总结一下常用命令. 1.连接及查看所有数据库 mongo 主机ip:端口号/连接的数据库名 -u 用户名 -p 密码 / ...

  3. nosql之mongodb的数据库操作+集合的插入和更新操作

    mongodb学习文档: https://docs.mongoing.com/ 一.数据库操作 1.数据库use存在就使用,不存就创建 use test1; 查看所有数据库 show dbs; 切换当 ...

  4. MongoDB 实战教程:数据库与集合的 CRUD 操作篇

    你好,我是悦创. 公众号:AI悦创 简介 MongoDB 是非关系型数据库(NoSQL)的代表之一,它具有高灵活.可水平扩展.高可用等优秀特性,并且它提供了强大又丰富的查询语句使开发者能够随心所欲地操 ...

  5. JAVA生成并导出json文件

    将一个list集合转换成json文件并导出: 数据集合: List<Object> agencyList = new ArrayList<Object>();Map<St ...

  6. navicat查询oracle表结构,利用Navicat Premium导出数据库表结构信息至Excel的方法

    因为要编写设计文档,需要用到数据库表结构,想要在word文档中以二维表格的形式展示,故借助Excel形成二维表格样式,还是很方便的样子! 1.新建查询,并将以下SQL语句修改至需要 SELECT CO ...

  7. Java json文件生成

    将一个map集合转换成json文件并导出: // 数据准备:JSONObject resData = new JSONObject();ArrayList<Map> list = new ...

  8. vue 虚拟服务器,vue+webpack项目中使用dev-server搭建虚拟服务器,请求json文件数据,实现先后台分离开发...

    在项目开发中,先后台分离,作了假数据,项目使用vue2.0重构,后台也推到重来了,为了避免耽误开发进程,我作了虚拟的数据请求,使用vue-cli脚手架搭建的项目文件中dev-server搭建虚拟api ...

  9. C#解析json文件的方法

    JSON(全称为JavaScript Object Notation) 是一种轻量级的数据交换格式.它是基于JavaScript语法标准的一个子集. JSON采用完全独立于语言的文本格式,可以很容易在 ...

  10. python读取和存入json文件

    将内容存入json文件 import json filename="C:/Users/13451/Desktop/captions_train.json" with open(fi ...

最新文章

  1. 编程语言介绍、python解释器执行代码的过程
  2. 6.extern “c”
  3. 全面解析并实现逻辑回归(Python)
  4. 饼图的引导线怎么加_4步学会EXCEL复合条饼图制作方法,让统计结果更直观!
  5. js和python交互_JSShell:一个基于python的交互式Shell
  6. TaskPaper教程——如何安装运行脚本?
  7. Kafka连接SparkStreaming的两种方式
  8. HTML创建几个边框,使用HTML5创建多个边框
  9. 【ELAMN预测】基于布谷鸟算法优化ELMAN神经网络实现数据回归预测matlab代码
  10. 高中数学建模优秀论文_高中数学建模优秀论文
  11. 戴尔计算机无法安装Win10,示例戴尔无法开机如何重装win10
  12. 【微信公众号后台基础能力接口对接】
  13. c语言 英文歌曲大赛,英文歌曲大赛活动方案
  14. 2018年度AI评选揭晓!10大领航企业,50家明星公司,10佳投资机构
  15. 2022年5月22日-Taylor级数的python实验
  16. [java] JavaMail发送邮件
  17. 支持邮件群发功能的邮箱有哪些?邮箱如何群发邮件,邮件群发怎么发呢?
  18. EasyRules动态规则实现
  19. 数据库之逻辑设计阶段(候选码、主码、外码、范式…)
  20. LINQ之Update

热门文章

  1. 【python】list,dict赋值不要用等号,要用extend,update
  2. 一封程序员的苦逼辞职信
  3. 机器学习基础:评价指标(Machine Learning Fundamentals: Evaluation Metrics)
  4. 研究生怎么看 ,怎么写论文
  5. Ample Sound Ample Guitar Semi Hollow Mac - 半空心体式吉他
  6. Mac菜单栏设置教程,教你更改顺序或隐藏APP图标
  7. Mac实用技巧:怎样使用终端在macOS Big Sur Finder中锁定文件!
  8. MAC下Android Studio快捷键的配置
  9. 一首歌是怎么诞生的?
  10. Linux 4.21优化Zen 2架构