mongodb的导入导出方法

移动开发 Android MongoDB
MongoDB提供了mongoexport工具,可以把一个collection导出成json格式或csv格式的文件。可以指定导出哪些数据项,也可以根据给定的条件导出数据。工具帮助信息如下:

(mongoexport导出工具

MongoDB提供了mongoexport工具,可以把一个collection导出成json格式或csv格式的文件。可以指定导出哪些数据项,也可以根据给定的条件导出数据。工具帮助信息如下:

  1. [root@localhost bin]# ./mongoexport --help  
  2. options:  
  3. --help produce help message   
  4. -v [ --verbose ] be more verbose (include multiple times for more   
  5. verbosity e.g. -vvvvv)   
  6. -h [ --host ] arg mongo host to connect to ( /s1,s2 for sets)   
  7. --port arg server port. Can also use --host hostname:port   
  8. --ipv6 enable IPv6 support (disabled by default)   
  9. -u [ --username ] arg username   
  10. -p [ --password ] arg password   
  11. --dbpath arg directly access mongod database files in the given   
  12. path, instead of connecting to a mongod server -   
  13. needs to lock the data directory, so cannot be used   
  14. if a mongod is currently accessing the same path   
  15. --directoryperdb if dbpath specified, each db is in a separate   
  16. directory   
  17. -d [ --db ] arg database to use   
  18. -c [ --collection ] arg collection to use (some commands)   
  19. -f [ --fields ] arg comma separated list of field names e.g. -f name,age   
  20. --fieldFile arg file with fields names - 1 per line   
  21. -q [ --query ] arg query filter, as a JSON string   
  22. --csv export to csv instead of json   
  23. -o [ --out ] arg output file; if not specified, stdout is used   
  24. --jsonArray output to a json array rather than one object per   
  25. line   
  26. [root@localhost bin]# 

下面我们将以一个实际的例子说明,此工具的用法:

将foo库中的表t1导出成json格式:

  1. [root@localhost bin]# ./mongoexport -d foo -c t1 -o /data/t1.json   
  2. connected to: 127.0.0.1   
  3. exported 1 records   
  4. [root@localhost bin]# 

导出成功后我们看一下/data/t1.json文件的样式,是否是我们所希望的:

  1. root@localhost data]# more t1.json   
  2. "_id" : { "$oid" : "4f927e2385b7a6814a0540a0" }, "age" : 2 }   
  3. [root@localhost data]# 

通过以上说明导出成功,但有一个问题,要是异构数据库的迁移怎么办呢?例如我们要将MongoDB的数据导入到MySQL该怎么办呢?MongoDB提供 了一种csv的导出格式,就可以解决异构数据库迁移的问题了. 下面将foo库的t2表的age和name列导出, 具体如下:

  1. [root@localhost bin]# ./mongoexport -d foo -c t2 --csv -f age,name -o /data/t2.csv   
  2. connected to: 127.0.0.1   
  3. exported 1 records   
  4. [root@localhost bin]# 

查看/data/t2.csv的导出结果

  1. [root@localhost data]# more t2.csv   
  2. age,name   
  3. 1,"wwl"   
  4. [root@localhost data]# 

mongoimport导入工具

MongoDB提供了mongoimport工具,可以把一个特定格式文件中的内容导入到某张collection中。工具帮助信息如下:

  1. [root@localhost bin]# ./mongoimport --help   
  2. options:   
  3. --help produce help message   
  4. -v [ --verbose ] be more verbose (include multiple times for more   
  5. verbosity e.g. -vvvvv)   
  6. -h [ --host ] arg mongo host to connect to ( /s1,s2 for sets)   
  7. --port arg server port. Can also use --host hostname:port   
  8. --ipv6 enable IPv6 support (disabled by default)   
  9. -u [ --username ] arg username   
  10. -p [ --password ] arg password   
  11. --dbpath arg directly access mongod database files in the given   
  12. path, instead of connecting to a mongod server -   
  13. needs to lock the data directory, so cannot be used   
  14. if a mongod is currently accessing the same path   
  15. --directoryperdb if dbpath specified, each db is in a separate   
  16. directory   
  17. -d [ --db ] arg database to use   
  18. -c [ --collection ] arg collection to use (some commands)   
  19. -f [ --fields ] arg comma separated list of field names e.g. -f name,age   
  20. --fieldFile arg file with fields names - 1 per line   
  21. --ignoreBlanks if given, empty fields in csv and tsv will be ignored   
  22. --type arg type of file to importdefault: json (json,csv,tsv)   
  23. --file arg file to import from; if not specified stdin is used   
  24. --drop drop collection first   
  25. --headerline CSV,TSV only - use first line as headers   
  26. --upsert insert or update objects that already exist   
  27. --upsertFields arg comma-separated fields for the query part of the   
  28. upsert. You should make sure this is indexed   
  29. --stopOnError stop importing at first error rather than continuing   
  30. --jsonArray load a json array, not one item per line. Currently   
  31. limited to 4MB. 

下面我们将以一人实际的例子说明,此工具的用法:
先看一下foo库中的t1表数据:

  1. > db.t1.find();   
  2. "_id" : ObjectId("4f937a56450beadc560feaa9"), "age" : 5 }   

t1其中有一条age=5的记录, 我们再看一下json文件中的数据是什么样子的:

  1. [root@localhost data]# more t1.json   
  2. "_id" : { "$oid" : "4f937a56450beadc560feaa7" }, "age" : 8 }   
  3. [root@localhost data]# 

可以看到t1.json文件中有一条age=8的数据,下面我们将用mongoimport工具将json文件中的记录导入到t1表中:

  1. [root@localhost bin]# ./mongoimport -d foo -c t1 /data/t1.json   
  2. connected to: 127.0.0.1   
  3. imported 1 objects 

工具返回信息说明向表中插入了一条记录. 我们进库里实际验证一下:

  1. [root@localhost bin]# ./mongo   
  2. MongoDB shell version: 1.8.1   
  3. connecting to: test   
  4. > use foo   
  5. switched to db foo   
  6. > db.t1.find();   
  7. "_id" : ObjectId("4f937a56450beadc560feaa9"), "age" : 5 }   
  8. "_id" : ObjectId("4f937a56450beadc560feaa7"), "age" : 8 }   

 

责任编辑:chenqingxiang 来源: oschina
相关推荐

2011-01-18 17:05:35

Thunderbird邮件导入导出

2011-05-16 13:05:56

SQL导入导出MYSQL

2011-04-13 10:09:50

Oracle数据泵导入导出

2011-05-24 09:51:07

MySQLMongoDB

2011-05-16 14:17:31

MySQL导入导出大量数据

2020-12-23 14:18:43

JavaScript模块导出

2010-08-26 16:49:09

DB2导入导出

2019-08-25 23:30:10

mysql命令mysqldump

2011-07-26 13:05:06

PLSQL DevelopOracle数据库

2010-10-28 11:55:47

oracle数据导出

2023-03-28 07:17:25

场景数据业务

2011-04-15 10:37:53

Oracle导入导出语法

2012-03-22 10:23:24

Exchange 20邮箱数据

2019-09-05 19:28:23

Docker程序员MySQL

2011-07-27 15:28:10

MySQL数据库字符编码集

2010-04-22 10:16:43

2009-11-23 10:24:22

2010-11-29 13:22:45

sybase数据表

2011-02-21 14:40:08

Foxmailoutlook数据

2010-07-23 09:25:50

SQL Server导
点赞
收藏

51CTO技术栈公众号