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

小編給大家分享一下導(dǎo)出MongoDB里數(shù)據(jù)的方法,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!

成都創(chuàng)新互聯(lián)是一家專注于成都做網(wǎng)站、成都網(wǎng)站設(shè)計、成都外貿(mào)網(wǎng)站建設(shè)與策劃設(shè)計,新吳網(wǎng)站建設(shè)哪家好?成都創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)10余年,網(wǎng)設(shè)計領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:新吳等地區(qū)。新吳做網(wǎng)站價格咨詢:18982081108

一、Mongodb導(dǎo)出工具mongoexport

Mongodb中的mongoexport工具可以把一個collection導(dǎo)出成JSON格式或CSV格式(類似于表格的形式)的文件??梢酝ㄟ^參數(shù)指定導(dǎo)出的數(shù)據(jù)項,也可以根據(jù)指定的條件導(dǎo)出數(shù)據(jù)。

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

參數(shù)說明:

-h:指明數(shù)據(jù)庫宿主機(jī)的IP

-u:指明數(shù)據(jù)庫的用戶名

-p:指明數(shù)據(jù)庫的密碼

-d:指明數(shù)據(jù)庫的名字

-c:指明collection的名字

-f:指明要導(dǎo)出那些列

-o:指明到要導(dǎo)出的文件名

-q:指明導(dǎo)出數(shù)據(jù)的過濾條件

二、常用數(shù)據(jù)導(dǎo)出實例

1、直接導(dǎo)出數(shù)據(jù)到文件中

代碼如下:

[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]#

命令執(zhí)行完后使用命令查看,會發(fā)現(xiàn)目錄下生成了一個students.dat的文件

參數(shù)說明:

-d 指明使用的庫, 本例中為” my_mongodb”

-c 指明要導(dǎo)出的表, 本例中為”user”

-o 指明要導(dǎo)出的文件名, 本例中為”user.dat”

從上面可以看到導(dǎo)出的方式使用的是JSON 的樣式

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

3、導(dǎo)出為CSV格式的數(shù)據(jù)

代碼如下:

[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é)果

代碼如下:

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、指定過濾條件導(dǎo)出數(shù)據(jù):

-q 參數(shù)的使用方法是:-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

看完了這篇文章,相信你對導(dǎo)出mongodb里數(shù)據(jù)的方法有了一定的了解,想了解更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

標(biāo)題名稱:導(dǎo)出mongodb里數(shù)據(jù)的方法
URL地址:http://bm7419.com/article36/iihssg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機(jī)、企業(yè)建站Google、動態(tài)網(wǎng)站、網(wǎng)站設(shè)計、標(biāo)簽優(yōu)化

廣告

聲明:本網(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)

成都app開發(fā)公司