云服务器免费试用

mongodb怎么导出脚本

服务器知识 0 811

mongodb导出脚本的方法:在mongodb中能够使用mongoexport工具将一个集合导出成JSON格式或CSV格式的文件,语法格式:“mongoexport -d 数据库 -c 集合名称 -o 文件路径”,该语句的参数说明:-d指明数据库的名字、-c指明collection的名字、-o:指明到要导出的文件名。

mongodb怎么导出脚本

具体内容如下:

Mongodb中的mongoexport工具可以把一个collection导出成JSON格式或CSV格式的文件。可以通过参数指定导出的数据项,也可以根据指定的条件导出数据。mongoexport具体用法如下所示:

[root@lb-vm5bin]#./mongoexport--help

Usage:mongoexport<options>

ExportdatafromMongoDBinCSVorJSONformat.

Seehttp://docs.mongodb.org/manual/reference/program/mongoexport/formoreinformation.

generaloptions:

--helpprintusage

--versionprintthetoolversionandexit

verbosityoptions:

-v,--verbose=<level>moredetailedlogoutput(includemultipletimesformoreverbosity,e.g.-vvvvv,orspecifyanumericvalue,e.g.--verbose=N)

--quiethidealllogoutput

connectionoptions:

-h,--host=<hostname>mongodbhosttoconnectto(setname/host1,host2forreplicasets)

--port=<port>serverport(canalsouse--hosthostname:port)

ssloptions:

--sslconnecttoamongodormongosthathassslenabled

--sslCAFile=<filename>the.pemfilecontainingtherootcertificatechainfromthecertificateauthority

--sslPEMKeyFile=<filename>the.pemfilecontainingthecertificateandkey

--sslPEMKeyPassword=<password>thepasswordtodecryptthesslPEMKeyFile,ifnecessary

--sslCRLFile=<filename>the.pemfilecontainingthecertificaterevocationlist

--sslAllowInvalidCertificatesbypassthevalidationforservercertificates

--sslAllowInvalidHostnamesbypassthevalidationforservername

--sslFIPSModeuseFIPSmodeoftheinstalledopenssllibrary

authenticationoptions:

-u,--username=<username>usernameforauthentication

-p,--password=<password>passwordforauthentication

--authenticationDatabase=<database-name>databasethatholdstheuser'scredentials

--authenticationMechanism=<mechanism>authenticationmechanismtouse

namespaceoptions:

-d,--db=<database-name>databasetouse

-c,--collection=<collection-name>collectiontouse

outputoptions:

-f,--fields=<field>[,<field>]*commaseparatedlistoffieldnames(requiredforexportingCSV)e.g.-f"name,age"

--fieldFile=<filename>filewithfieldnames-1perline

--type=<type>theoutputformat,eitherjsonorcsv(defaultsto'json')

-o,--out=<filename>outputfile;ifnotspecified,stdoutisused

--jsonArrayoutputtoaJSONarrayratherthanoneobjectperline

--prettyoutputJSONformattedtobehuman-readable

queryingoptions:

-q,--query=<json>queryfilter,asaJSONstring,e.g.,'{x:{$gt:1}}'

--queryFile=<filename>pathtoafilecontainingaqueryfilter(JSON)

-k,--slaveOkallowsecondaryreadsifavailable(defaulttrue)

--readPreference=<string>|<json>specifyeitherapreferencenameorapreferencejsonobject

--forceTableScanforceatablescan(donotuse$snapshot)

--skip=<count>numberofdocumentstoskip

--limit=<count>limitthenumberofdocumentstoexport

--sort=<json>sortorder,asaJSONstring,e.g.'{x:1}'

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

./mongoexport-duser_center-cpassport-udev-p123456-opassport.json

//摩拜单车mongodb查询语句导出以201612开头时间小于1482170579000状态为RENTAL

./bin/mongoexport-d数据库-c集合-u用户名-p密码-q'{bikeRentalStatus:"RENTAL",times:{$regex:/201612.*/i},systimes:{$lt:NumberLong(1482170579000)}}'-o1.jaon

./bin/mongoexport-duser_center-cpassport-udev-p*****-q'{_id:{$in:[171]}}'-o1.jaon

参数说明:

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

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

-p:指明数据库的密码

-d:指明数据库的名字

-c:指明collection的名字

-f:指明要导出那些列

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

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

-d:指明使用的库

-c:指明要导出的集合

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

从上面的结果可以看出,我们在导出数据时没有显示指定导出样式 ,默认导出了JSON格式的数据。如果我们需要导出CSV格式的数据,则需要使用–csv参数

./mongoexport-duser_center-cuser_name-udev-p123456--csv-ouser_name.json

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942@qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: mongodb怎么导出脚本
本文地址: https://solustack.com/46368.html

相关推荐:

网友留言:

我要评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。