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
相关推荐
Databend1 小时前
Databend 亮相 RustChinaConf 2025,分享基于 Rust 构建商业化数仓平台的探索
数据库
得物技术2 小时前
破解gh-ost变更导致MySQL表膨胀之谜|得物技术
数据库·后端·mysql
似水流年流不尽思念5 小时前
MongoDB 有哪些索引?适用场景?
后端·mongodb
Raymond运维7 小时前
MariaDB源码编译安装(二)
运维·数据库·mariadb
沢田纲吉7 小时前
🗄️ MySQL 表操作全面指南
数据库·后端·mysql
RestCloud1 天前
SQL Server到Hive:批处理ETL性能提升30%的实战经验
数据库·api
RestCloud1 天前
为什么说零代码 ETL 是未来趋势?
数据库·api
ClouGence1 天前
CloudCanal + Paimon + SelectDB 从 0 到 1 构建实时湖仓
数据库
DemonAvenger1 天前
NoSQL与MySQL混合架构设计:从入门到实战的最佳实践
数据库·mysql·性能优化
AAA修煤气灶刘哥2 天前
后端人速藏!数据库PD建模避坑指南
数据库·后端·mysql