mongodb導(dǎo)入和導(dǎo)出數(shù)據(jù)的方法

這篇文章主要介紹“MongoDB導(dǎo)入和導(dǎo)出數(shù)據(jù)的方法”,在日常操作中,相信很多人在mongodb導(dǎo)入和導(dǎo)出數(shù)據(jù)的方法問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”mongodb導(dǎo)入和導(dǎo)出數(shù)據(jù)的方法”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

西工ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為成都創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:028-86922220(備注:SSL證書合作)期待與您的合作!

MongoDB提供了mongoexport工具,可以把一個collection導(dǎo)出成json格式或csv格式的文件??梢灾付▽?dǎo)出哪些數(shù)據(jù)項,也可以根據(jù)給定的條件導(dǎo)出數(shù)據(jù)。工具幫助信息如下:

[root@localhost 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 ( /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導(dǎo)出成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]#

導(dǎo)出成功后我們看一下/data/t1.json文件的樣式,是否是我們所希望的:

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

通過以上說明導(dǎo)出成功,但有一個問題,要是異構(gòu)數(shù)據(jù)庫的遷移怎么辦呢?例如我們要將MongoDB的數(shù)據(jù)導(dǎo)入到MySQL該怎么辦呢?MongoDB提供 了一種csv的導(dǎo)出格式,就可以解決異構(gòu)數(shù)據(jù)庫遷移的問題了. 下面將foo庫的t2表的age和name列導(dǎo)出, 具體如下:

[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的導(dǎo)出結(jié)果

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

mongoimport導(dǎo)入工具

MongoDB提供了mongoimport工具,可以把一個特定格式文件中的內(nèi)容導(dǎo)入到某張collection中。工具幫助信息如下:

[root@localhost bin]# ./mongoimport --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 ( /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   --ignoreBlanks if given, empty fields in csv and tsv will be ignored   --type arg type of file to import. default: json (json,csv,tsv)   --file arg file to import from; if not specified stdin is used   --drop drop collection first   --headerline CSV,TSV only - use first line as headers   --upsert insert or update objects that already exist   --upsertFields arg comma-separated fields for the query part of the   upsert. You should make sure this is indexed   --stopOnError stop importing at first error rather than continuing   --jsonArray load a json array, not one item per line. Currently   limited to 4MB.

下面我們將以一人實際的例子說明,此工具的用法:  
先看一下foo庫中的t1表數(shù)據(jù):

> db.t1.find();   { "_id" : ObjectId("4f937a56450beadc560feaa9"), "age" : 5 }   >

t1其中有一條age=5的記錄, 我們再看一下json文件中的數(shù)據(jù)是什么樣子的:

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

可以看到t1.json文件中有一條age=8的數(shù)據(jù),下面我們將用mongoimport工具將json文件中的記錄導(dǎo)入到t1表中:

[root@localhost bin]# ./mongoimport -d foo -c t1 /data/t1.json   connected to: 127.0.0.1   imported 1 objects

工具返回信息說明向表中插入了一條記錄. 我們進(jìn)庫里實際驗證一下:

[root@localhost bin]# ./mongo   MongoDB shell version: 1.8.1   connecting to: test   > use foo   switched to db foo   > db.t1.find();   { "_id" : ObjectId("4f937a56450beadc560feaa9"), "age" : 5 }   { "_id" : ObjectId("4f937a56450beadc560feaa7"), "age" : 8 }   >

到此,關(guān)于“mongodb導(dǎo)入和導(dǎo)出數(shù)據(jù)的方法”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

新聞名稱:mongodb導(dǎo)入和導(dǎo)出數(shù)據(jù)的方法
新聞來源:http://muchs.cn/article24/pphhce.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊、虛擬主機(jī)、服務(wù)器托管定制開發(fā)、微信小程序網(wǎng)站導(dǎo)航

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)公司