深入分析Mongodb資料的匯入匯出_MongoDB

來源:互聯網
上載者:User

一、Mongodb匯出工具mongoexport

Mongodb中的mongoexport工具可以把一個collection匯出成JSON格式或CSV格式的檔案。可以通過參數指定匯出的資料項目,也可以根據指定的條件匯出資料。
mongoexport具體用法

 [root@localhost mongodb]# ./bin/mongoexport --help Export MongoDB data to CSV, TSV or JSON files. options:  --help          produce help message  -v [ --verbose ]     be more verbose (include multiple times for more                verbosity e.g. -vvvvv)  --version         print the program's version and exit  -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  --journal         enable journaling  -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  -k [ --slaveOk ] arg (=1) use secondaries for export if available, default                true 

參數說明
-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的匯出結果

複製代碼 代碼如下:

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

三、Mongodb匯入工具mongoexport

Mongodb中的mongoimport工具可以把一個特定格式檔案中的內容匯入到指定的collection中。該工具可以匯入JSON格式資料,也可以匯入CSV格式資料。
 
具體使用如下

 [root@localhost mongodb]# ./bin/mongoimport --help options:  --help         produce help message  -v [ --verbose ]    be more verbose (include multiple times for more               verbosity e.g. -vvvvv)  --version        print the program's version and exit  -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  --journal        enable journaling  -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. 

參數說明
 
-h:指明資料庫宿主機的IP
-u:指明資料庫的使用者名稱
-p:指明資料庫的密碼
-d:指明資料庫的名字
-c:指明collection的名字
-f:指明要匯入那些列

四、常用資料匯入執行個體

1、匯入JSON 資料

複製代碼 代碼如下:

[root@localhost mongodb]# ./bin/mongoimport -d test -c students students.dat  
connected to: 127.0.0.1 
imported 9 objects 
[root@localhost mongodb]#  

參數說明
-d:指明資料庫名,本例中為test
-c:指明collection名,本例中為students
students.dat:匯入的檔案名稱

2、匯入CSV格式檔案中的內容

複製代碼 代碼如下:

[root@localhost mongodb]# ./bin/mongoimport -d test -c students --type csv --headerline --file students_csv.dat 
connected to: 127.0.0.1
imported 10 objects
[root@localhost mongodb]# 

參數說明:
-type:指明要匯入的檔案格式
-headerline:指明第一行是列名,不需要匯入
-file:指明要匯入的檔案

以上所述就是本文的全部內容了,希望大家能夠喜歡。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.