温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

DBA应用技巧中怎样利用MySQL技能学习MongoDB

发布时间:2021-09-29 10:35:47 来源:亿速云 阅读:108 作者:柒染 栏目:数据库

DBA应用技巧中怎样利用MySQL技能学习MongoDB,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

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

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

  下面我们将以一个实际的例子说明,此工具的用法:
  将foo库中的表t1导出成json格式:

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

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

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

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

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

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

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

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI