本文导读
- 项目开发或者部署时,经常会遇到需要将 MongoDB 数据导出为其他格式数据文件,或从其他格式数据中导入到MongoDB。
MongoDB 数据导出
MongoDB 数据导出json
[root@localhost ~]# mongoexport -d local -c startup_log -o /root/export.json
2017-09-02T23:20:40.895+0800 connected to: localhost
2017-09-02T23:20:40.898+0800 exported 5 records
MongoDB 数据导出csv
[root@localhost ~]# mongoexport -d local -c startup_log --type=csv -f hostname,startTime -o /root/export.csv
2017-09-02T23:29:01.585+0800 connected to: localhost
2017-09-02T23:29:01.585+0800 exported 5 records
MongoDB 数据导入
- 1、首先新建一个 collection:test_log
> use local
switched to db local
> db.createCollection('test_log')
{ "ok" : 1 }
> show collections
startup_log
test_log
MongoDB 导入json数据
[root@localhost ~]# mongoimport -d local -c test_log --file export.json
2017-09-02T23:35:23.379+0800 connected to: localhost
2017-09-02T23:35:23.408+0800 imported 5 documents
MongoDB 导入csv数据
[root@localhost ~]# mongoimport -d local -c test_log --type csv --headerline --file export.csv
2017-09-02T23:36:37.462+0800 connected to: localhost
2017-09-02T23:36:37.480+0800 imported 5 documents