教你如何利用MySQL学习MongoDB之导入和导出

发表于:2011-10-08 10:05

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:王文龙    来源:51Testing软件测试网采编

  导出成功后我们看一下/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]#

  可以看出MongoDB为我们提供了一个强在的数据导出工具。

  (2)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 import. default: 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.

32/3<123>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号