mongodb通过mongoexport命令导出数据

一、mongoexport命令参数

我们通过mongoexport --help来查看这个命令支持的参数

二、mongoexport几个常用参数的演示

2.1、导出所有数据,格式为json格式

--type 用来指定导出的数据格式,可以导出为.json或者.csv

powershell 复制代码
mongoexport --host localhost --port 27017 --db  solitaire_test --collection my_solitaire  --type=json --out /Users/jiagang/work-jag/mongo/mongooutput.json

结果

powershell 复制代码
root@bddff4197a79:/# mongoexport --host localhost --port 27017 --db  solitaire_test --collection my_solitaire  --type=json --out /Users/jiagang/work-jag/mongo/mongooutput.json
2023-11-09T02:09:46.963+0000	connected to: mongodb://localhost:27017/
2023-11-09T02:09:46.979+0000	exported 106 records
2.2、通过--query筛选得到目标数据并导出为json格式

条件为userId为zhangsan的

powershell 复制代码
mongoexport --host localhost --port 27017 --db  solitaire_test --collection my_solitaire --query  '{"userId":"zhangsan"}' --type=json --out /Users/jiagang/work-jag/mongo/mongooutput.json

结果

powershell 复制代码
root@bddff4197a79:/# mongoexport --host localhost --port 27017 --db  solitaire_test --collection my_solitaire --query '{"userId":"zhangsan"}'  --type=json --out /Users/jiagang/work-jag/mongo/mongooutput.json
2023-11-09T02:39:48.983+0000	connected to: mongodb://localhost:27017/
2023-11-09T02:39:48.989+0000	exported 1 record
2.3、通过--query筛选得到目标数据,并展示某个字段,导出为csv格式

-f userName 表示导出的数据只有userName字段,多个字段用逗号隔开

powershell 复制代码
root@bddff4197a79:/tmp# mongoexport --host localhost --port 27017 --db solitaire_test --collection my_solitaire --query  '{"userId":"zhangsan"}'  -f userName  --type=csv  --out /Users/jiagang/work-jag/mongo/mongooutput.csv
2023-11-09T02:44:26.766+0000	connected to: mongodb://localhost:27017/
2023-11-09T02:44:26.773+0000	exported 1 record

结果

powershell 复制代码
root@bddff4197a79:/tmp# mongoexport --host localhost --port 27017 --db solitaire_test --collection my_solitaire --query  '{"userId":"zhangsan"}'  -f userName  --type=csv  --out /Users/jiagang/work-jag/mongo/mongooutput.csv
2023-11-09T02:44:26.766+0000	connected to: mongodb://localhost:27017/
2023-11-09T02:44:26.773+0000	exported 1 record

查看mongooutput.csv文件

2.4、携带账号密码的导出
powershell 复制代码
mongoexport --host localost --port 27017 --username root  --password 'yourpassword' --authenticationDatabase admin  --db yourdb  --collection yourcollection   --type=json  --out /tmp/mongooutput.json
相关推荐
ClouGence6 小时前
Oracle 数据同步为什么会出现数据不一致?长事务是常被忽略的原因
数据库·后端·oracle
飞将9 小时前
从零实现数据库(2)——HashIndex + IndexManager
数据库
Nturmoils1 天前
订单列表慢查询,先看 WHERE、ORDER BY 和 LIMIT
数据库
渣波1 天前
拒绝 SQL 焦虑!手把手带你用 NestJS + Prisma + DTO 写出“防弹”级后端代码
javascript·数据库·后端
葫芦和十三1 天前
图解 MongoDB 08|ESR 原则:复合索引的字段顺序怎么定
后端·mongodb·agent
葫芦和十三2 天前
图解 MongoDB 07|索引类型:七种索引,七种访问形状
后端·mongodb·agent
倔强的石头_2 天前
KingbaseES 新版MySQL 兼容版体验:旧版迁移 + 功能实测
数据库
葫芦和十三2 天前
图解 MongoDB 06|模式演进:无 schema 是优势还是债
后端·mongodb·agent
葫芦和十三3 天前
图解 MongoDB 05|文档模型设计:内嵌 vs 引用,反范式不是免费午餐
后端·mongodb·agent
zzzzzz3103 天前
9K Star 炸裂开源!这个 C 语言写的代码知识图谱,把 Linux 内核索引压缩到了 3 分钟
linux·服务器·sql