温馨提示×

温馨提示×

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

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

导出mongodb里数据的方法

发布时间:2020-07-30 14:37:51 来源:亿速云 阅读:234 作者:清晨 栏目:编程语言

小编给大家分享一下导出mongodb里数据的方法,希望大家阅读完这篇文章后大所收获,下面让我们一起去探讨吧!

一、Mongodb导出工具mongoexport

Mongodb中的mongoexport工具可以把一个collection导出成JSON格式或CSV格式(类似于表格的形式)的文件。可以通过参数指定导出的数据项,也可以根据指定的条件导出数据。

mongoexport具体用法

C:\mongo\bin>mongoexport -help   options:     --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

参数说明:

-h:指明数据库宿主机的IP

-u:指明数据库的用户名

-p:指明数据库的密码

-d:指明数据库的名字

-c:指明collection的名字

-f:指明要导出那些列

-o:指明到要导出的文件名

-q:指明导出数据的过滤条件

二、常用数据导出实例

1、直接导出数据到文件中

代码如下:

[root@localhost bin]# ./mongoexport -d my_mongodb -c user -o user.dat connected to: 127.0.0.1 exported 2 records [root@localhost bin]# cat user.dat { "_id" : { "$oid" : "4f81a4a1779282ca68fd8a5a" }, "uid" : 2, "username" : "Jerry", "age" : 100 } { "_id" : { "$oid" : "4f844d1847d25a9ce5f120c4" }, "uid" : 1, "username" : "Tom", "age" : 25 } [root@localhost bin]#

命令执行完后使用命令查看,会发现目录下生成了一个students.dat的文件

参数说明:

-d 指明使用的库, 本例中为” my_mongodb”

-c 指明要导出的表, 本例中为”user”

-o 指明要导出的文件名, 本例中为”user.dat”

从上面可以看到导出的方式使用的是JSON 的样式

2、将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]#

3、导出为CSV格式的数据

代码如下:

[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的导出结果

代码如下:

mongoexport -h 10.100.30.130 --port 27017 -d zhongtudao -c hand_result --type=csv -f datetime,url,show_count,click_count -q '{datetime:"20170402"}' -o ./20170402.csv

4、指定过滤条件导出数据:

-q 参数的使用方法是:-q '{key:"value"}'

代码如下:

mongoexport -h 10.100.30.130 --port 27017 -d zhongtudao -c hand_result --type=csv -f datetime,url,show_count,click_count -q '{datetime:"20170402"}' -o ./20170402.csv

看完了这篇文章,相信你对导出mongodb里数据的方法有了一定的了解,想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

向AI问一下细节

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

AI