一、导出工具mongoexport

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

Shell代码  

1. [root@localhost mongodb]# ./bin/mongoexport --help  
2. Export MongoDB data to CSV, TSV or JSON files.  
3.   
4. options:  
5.   --help                    produce help message  
6.   -v [ --verbose ]          be more verbose (include multiple times for more   
7.                             verbosity e.g. -vvvvv)  
8.   --version                 print the program's version and exit  
9.   -h [ --host ] arg         mongo host to connect to ( <set name>/s1,s2 for   
10.                             sets)  
11.   --port arg                server port. Can also use --host hostname:port  
12.   --ipv6                    enable IPv6 support (disabled by default)  
13.   -u [ --username ] arg     username  
14.   -p [ --password ] arg     password  
15.   --dbpath arg              directly access mongod database files in the given   
16.                             path, instead of connecting to a mongod  server -   
17.                             needs to lock the data directory, so cannot be used  
18.                             if a mongod is currently accessing the same path  
19.   --directoryperdb          if dbpath specified, each db is in a separate   
20.                             directory  
21.   --journal                 enable journaling  
22.   -d [ --db ] arg           database to use  
23.   -c [ --collection ] arg   collection to use (some commands)  
24.   -f [ --fields ] arg       comma separated list of field names e.g. -f   
25.                             name,age  
26. 1 per line  
27.   -q [ --query ] arg        query filter, as a JSON string  
28.   --csv                     export to csv instead of json  
29.   -o [ --out ] arg          output file; if not specified, stdout is used  
30.   --jsonArray               output to a json array rather than one object per   
31.                             line  
32. 1) use secondaries for export if available, default   
33.                             true

参数说明:

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

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

-p:指明数据库的密码

-d:指明数据库的名字

-c:指明collection的名字

-f:指明要导出那些列

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

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

 

实例:test库中存在着一个students集合,集合中数据如下:

Js代码  

1. > db.students.find()  
2. { "_id" : ObjectId("5031143350f2481577ea81e5"), "classid" : 1, "age" : 20, "name" : "kobe" }  
3. { "_id" : ObjectId("5031144a50f2481577ea81e6"), "classid" : 1, "age" : 23, "name" : "nash" }  
4. { "_id" : ObjectId("5031145a50f2481577ea81e7"), "classid" : 2, "age" : 18, "name" : "james" }  
5. { "_id" : ObjectId("5031146a50f2481577ea81e8"), "classid" : 2, "age" : 19, "name" : "wade" }  
6. { "_id" : ObjectId("5031147450f2481577ea81e9"), "classid" : 2, "age" : 19, "name" : "bosh" }  
7. { "_id" : ObjectId("5031148650f2481577ea81ea"), "classid" : 2, "age" : 25, "name" : "allen" }  
8. { "_id" : ObjectId("5031149b50f2481577ea81eb"), "classid" : 1, "age" : 19, "name" : "howard" }  
9. { "_id" : ObjectId("503114a750f2481577ea81ec"), "classid" : 1, "age" : 22, "name" : "paul" }  
10. { "_id" : ObjectId("503114cd50f2481577ea81ed"), "classid" : 2, "age" : 24, "name" : "shane" }

由上可以看出文档中存在着3个字段:classid、age、name

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

Shell代码  

1. [root@localhost mongodb]# ./bin/mongoexport -d test -c students -o students.dat  
2. connected to: 127.0.0.1  
3. exported 9 records

  

命令执行完后使用ll命令查看,发现目录下生成了一个students.dat的文件

Shell代码  

1. -rw-r--r-- 1 root root   869 Aug 21 00:05 students.dat

查看该文件信息,具体信息如下:

Shell代码  

1. [root@localhost mongodb]# cat students.dat   
2. { "_id" : { "$oid" : "5031143350f2481577ea81e5" }, "classid" : 1, "age" : 20, "name" : "kobe" }  
3. { "_id" : { "$oid" : "5031144a50f2481577ea81e6" }, "classid" : 1, "age" : 23, "name" : "nash" }  
4. { "_id" : { "$oid" : "5031145a50f2481577ea81e7" }, "classid" : 2, "age" : 18, "name" : "james" }  
5. { "_id" : { "$oid" : "5031146a50f2481577ea81e8" }, "classid" : 2, "age" : 19, "name" : "wade" }  
6. { "_id" : { "$oid" : "5031147450f2481577ea81e9" }, "classid" : 2, "age" : 19, "name" : "bosh" }  
7. { "_id" : { "$oid" : "5031148650f2481577ea81ea" }, "classid" : 2, "age" : 25, "name" : "allen" }  
8. { "_id" : { "$oid" : "5031149b50f2481577ea81eb" }, "classid" : 1, "age" : 19, "name" : "howard" }  
9. { "_id" : { "$oid" : "503114a750f2481577ea81ec" }, "classid" : 1, "age" : 22, "name" : "paul" }  
10. { "_id" : { "$oid" : "503114cd50f2481577ea81ed" }, "classid" : 2, "age" : 24, "name" : "shane" }

参数说明:

-d:指明使用的库,本例中为test

-c:指明要导出的集合,本例中为students

-o:指明要导出的文件名,本例中为students.dat

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

Shell代码  

1. [root@localhost mongodb]# ./bin/mongoexport -d test -c students --csv -f classid,name,age -o students_csv.dat  
2. connected to: 127.0.0.1  
3. exported 9 records  
4. [root@localhost mongodb]# cat students_csv.dat   
5. classid,name,age  
6. 1.0,"kobe",20.0  
7. 1.0,"nash",23.0  
8. 2.0,"james",18.0  
9. 2.0,"wade",19.0  
10. 2.0,"bosh",19.0  
11. 2.0,"allen",25.0  
12. 1.0,"howard",19.0  
13. 1.0,"paul",22.0  
14. 2.0,"shane",24.0  
15. [root@localhost mongodb]#

参数说明:

-csv:指明要导出为csv格式

-f:指明需要导出classid、name、age这3列的数据

由上面结果可以看出,mongoexport成功地将数据根据csv格式导出到了students_csv.dat文件中。

 

二、导入工具mongoimport

Mongodb中的mongoimport工具可以把一个特定格式文件中的内容导入到指定的collection中。该工具可以导入JSON格式数据,也可以导入CSV格式数据。具体使用如下所示:

Shell代码  

1. [root@localhost mongodb]# ./bin/mongoimport --help  
2. options:  
3.   --help                  produce help message  
4.   -v [ --verbose ]        be more verbose (include multiple times for more   
5.                           verbosity e.g. -vvvvv)  
6.   --version               print the program's version and exit  
7.   -h [ --host ] arg       mongo host to connect to ( <set name>/s1,s2 for sets)  
8.   --port arg              server port. Can also use --host hostname:port  
9.   --ipv6                  enable IPv6 support (disabled by default)  
10.   -u [ --username ] arg   username  
11.   -p [ --password ] arg   password  
12.   --dbpath arg            directly access mongod database files in the given   
13.                           path, instead of connecting to a mongod  server -   
14.                           needs to lock the data directory, so cannot be used   
15.                           if a mongod is currently accessing the same path  
16.   --directoryperdb        if dbpath specified, each db is in a separate   
17.                           directory  
18.   --journal               enable journaling  
19.   -d [ --db ] arg         database to use  
20.   -c [ --collection ] arg collection to use (some commands)  
21.   -f [ --fields ] arg     comma separated list of field names e.g. -f name,age  
22. 1 per line  
23.   --ignoreBlanks          if given, empty fields in csv and tsv will be ignored  
24.   --type arg              type of file to import.  default: json (json,csv,tsv)  
25.   --file arg              file to import from; if not specified stdin is used  
26.   --drop                  drop collection first   
27.   --headerline            CSV,TSV only - use first line as headers  
28.   --upsert                insert or update objects that already exist  
29.   --upsertFields arg      comma-separated fields for the query part of the   
30.                           upsert. You should make sure this is indexed  
31.   --stopOnError           stop importing at first error rather than continuing  
32.   --jsonArray             load a json array, not one item per line. Currently   
33.                           limited to 4MB.

参数说明:

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

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

-p:指明数据库的密码

-d:指明数据库的名字

-c:指明collection的名字

-f:指明要导入那些列

 

示例:先删除students中的数据,并验证

Js代码  

1. > db.students.remove()  
2. > db.students.find()  
3. >

然后再导入上面导出的students.dat文件中的内容

Shell代码  

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

参数说明:

-d:指明数据库名,本例中为test

-c:指明collection名,本例中为students

students.dat:导入的文件名

查询students集合中的数据

Js代码  

1. > db.students.find()  
2. { "_id" : ObjectId("5031143350f2481577ea81e5"), "classid" : 1, "age" : 20, "name" : "kobe" }  
3. { "_id" : ObjectId("5031144a50f2481577ea81e6"), "classid" : 1, "age" : 23, "name" : "nash" }  
4. { "_id" : ObjectId("5031145a50f2481577ea81e7"), "classid" : 2, "age" : 18, "name" : "james" }  
5. { "_id" : ObjectId("5031146a50f2481577ea81e8"), "classid" : 2, "age" : 19, "name" : "wade" }  
6. { "_id" : ObjectId("5031147450f2481577ea81e9"), "classid" : 2, "age" : 19, "name" : "bosh" }  
7. { "_id" : ObjectId("5031148650f2481577ea81ea"), "classid" : 2, "age" : 25, "name" : "allen" }  
8. { "_id" : ObjectId("5031149b50f2481577ea81eb"), "classid" : 1, "age" : 19, "name" : "howard" }  
9. { "_id" : ObjectId("503114a750f2481577ea81ec"), "classid" : 1, "age" : 22, "name" : "paul" }  
10. { "_id" : ObjectId("503114cd50f2481577ea81ed"), "classid" : 2, "age" : 24, "name" : "shane" }  
11. >

证明数据导入成功

上面演示的是导入JSON格式的文件中的内容,如果要导入CSV格式文件中的内容,则需要通过--type参数指定导入格式,具体如下所示:

先删除数据

Js代码  

1. > db.students.remove()  
2. > db.students.find()  
3. >

再导入之前导出的students_csv.dat文件

 

Shell代码  

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

参数说明:

-type:指明要导入的文件格式

-headerline:指明第一行是列名,不需要导入

-file:指明要导入的文件

查询students集合,验证导入是否成功:

 

Js代码  

1. > db.students.find()  
2. { "_id" : ObjectId("503266029355c632cd118ad8"), "classid" : 1, "name" : "kobe", "age" : 20 }  
3. { "_id" : ObjectId("503266029355c632cd118ad9"), "classid" : 1, "name" : "nash", "age" : 23 }  
4. { "_id" : ObjectId("503266029355c632cd118ada"), "classid" : 2, "name" : "james", "age" : 18 }  
5. { "_id" : ObjectId("503266029355c632cd118adb"), "classid" : 2, "name" : "wade", "age" : 19 }  
6. { "_id" : ObjectId("503266029355c632cd118adc"), "classid" : 2, "name" : "bosh", "age" : 19 }  
7. { "_id" : ObjectId("503266029355c632cd118add"), "classid" : 2, "name" : "allen", "age" : 25 }  
8. { "_id" : ObjectId("503266029355c632cd118ade"), "classid" : 1, "name" : "howard", "age" : 19 }  
9. { "_id" : ObjectId("503266029355c632cd118adf"), "classid" : 1, "name" : "paul", "age" : 22 }  
10. { "_id" : ObjectId("503266029355c632cd118ae0"), "classid" : 2, "name" : "shane", "age" : 24 }  
11. >

说明已经导入成功