当前位置: 首页 > 科技观察

mongodb的导入导出方法_0

时间:2023-03-15 09:58:15 科技观察

(mongoexport导出工具MongoDB提供了mongoexport工具,可以将一个集合导出成json格式或者csv格式的文件。可以指定导出哪些数据项,也可以根据导出数据给定条件。工具帮助信息如下:[root@localhostbin]#./mongoexport--helpoptions:--helpproducehelpmessage-v[--verbose]bemoreverbose(includemultipletimesformoreverbositye.g.-vvvvv)-h[--host]argmongohosttoconnectto(/s1,s2forsets)--portargserverport。也可以使用--hosthostname:port--ipv6enableIPv6support(默认禁用)-u[--username]argusername-p[--password]argpassword--dbpatharg直接访问给定路径中的mongod数据库文件,而不是连接到mongod服务器-需要锁定数据目录,因此不能使用difamongodb当前访问指定路径中的相同路径--db]argdatabasetouse-c[--collection]argcollectiontouse(一些命令)-f[--fields]argcommaseparatedlistoffieldnamese.g.-fname,age--fieldFileargfilewithfieldsnames-1perline-q[--query]argqueryfilter,asaJSONstring--csvexporttocsvinsteadofjson-o[--out]argoutputfile;ifnotspecified,stdoutisused--jsonArrayoutputtoajsonarrayratherthanoneobjectperline[root@localhostbin]#下面我们将以一个现实的例子本工具的说明、使用方法:将foo库中的表t1导出为json格式:[root@localhostbin]#./mongoexport-dfoo-ct1-o/data/t1.jsonconnectedto:127.0.0.1exported1records[root@localhostbin]#导出成功后,我们看看/data/t1.json文件的样式,是不是我们想要的:root@localhostdata]#moret1.json{"_id":{"$oid":"4f927e2385b7a6814a0540a0"},"age":2}[root@localhostdata]#通过上面的指令导出成功,但是有个问题,如果要迁移异构数据库怎么办?比如我们要将MongoDB的数据导入到MySQL中怎么办?MongoDB提供了一种csv导出格式可以解决异构数据库迁移的问题。接下来导出foo库的t2表的age和name列,如下:[root@localhostbin]#./mongoexport-dfoo-ct2--csv-fage,name-o/data/t2.csvconnectedto:127.0.0.1exported1records[root@localhostbin]#查看/data/t2.csv的导出结果[root@localhostdata]#moret2.csvage,name1,"wwl"[root@localhostdata]#mongoimport导入工具MongoDB提供了mongoimport工具,它可以将特定格式的文件内容导入到集合中工具帮助信息如下:[root@localhostbin]#./mongoimport--helpoptions:--helpproducehelpmessage-v[--verbose]bemoreverbose(includemultipletimesformoreverbositye.g.-vvvvv)-h[--host]argmongohosttoconnectto(/s1,s2forsets)--portargserverport。也可以使用--hosthostname:port--ipv6enableIPv6support(默认禁用)-u[--username]argusername-p[--password]argpassword--dbpatharg直接访问给定路径中的mongod数据库文件,而不是连接到mongod服务器-需要锁定数据目录,因此不能使用difamongodb当前访问指定路径中的相同路径--db]argdatabasetouse-c[--collection]argcollectiontouse(somecommands)-f[--fields]argcommaseparatedlistoffieldnamese.g.-fname,age--fieldFileargfilewithfieldsnames-1perline--ignoreBlanksifgiven,emptyfieldsincsvandtsvwillbeignored--typeargtypeoffiletoimport.default:json(json,csv,tsv)--filearg要导入的文件;如果未指定stdinisused--dropdropcollectionfirst--headerlineCSV,TSVonly-usefirstlineasheaders--upsertinsertorupdateobjectsthatalreadyexist--upsertFieldsargcomma-separatedfieldsforthequerypartoftheupsert.Youshouldmakesurethisisindexed--stopOnErrorstopimportingatfirsterrorratherthancontinuing--jsonArrayloadajsonarray,notoneitemperline.Currentlylimitedto4MB.下面我们将以一人实际的例子说明,此工具的用法:先看一下foo库中的t1表数据:>db.t1.find();{"_id":ObjectId("4f937a56450beadc560feaa9"),"age":5}>Thereisarecordofage=5int1,let'sseewhatthedatainthejsonfilelookslikeOf:[root@localhostdata]#moret1.json{"_id":{"$oid":"4f937a56450beadc560feaa7"},"age":8}[root@localhostdata]#Youcanseethatthereisanageinthet1.jsonfile=8data,wewillusethemongoimporttooltoimporttherecordsinthejsonfileintothet1table:[root@localhostbin]#./mongoimport-dfoo-ct1/data/t1.jsonconnectedto:127.0.0.1imported1objectsThetoolreturnsinformationItmeansthatarecordhasbeeninsertedintothetable.Let'sgointothelibraryandverifyit:[root@localhostbin]#./mongoMongoDBshellversion:1.8.1connectingto:test>usefooswitchedtodbfoo>db.t1.find();{"_id":ObjectId("4f937a56450beadc560feaa9"),"age":5}{"_id":ObjectId("4f937a56450beadc560feaa7"),"age":8}>