MongoDB导入(mongoimport)-导出(mongoexport)工具使用

1、导出工具-mongoexport

Mongodb中的mongoexport工具可以把一个collection导出为JSON格式或者CSV格式文件。

(1)、导出为JSON文件

/opt/mongodb/bin/mongoexport -h 127.0.0.1 -u user -p password --port 27017 -d dev_test_db -c dev_test_history_location  -q '{"handle" : "30487"}' -o history_location_30487.dat

(2)、导出为CSV文件

/opt/mongodb/bin/mongoexport -u user -p password --port 27017 -d dev_test_db -c dev_test_location --csv -q '{"handle" : "30439"}' -o dev_test_location_30439.csv --field type,handle,create_time

具体参数说明如下:

 1 Options:
 2   --help                                produce help message
 3   -v [ --verbose ]                      be more verbose (include multiple times
 4                                         for more verbosity e.g. -vvvvv)
 5   --quiet                               silence all non error diagnostic 
 6                                         messages
 7   --version                             print the program's version and exit
 8   -h [ --host ] arg                     mongo host to connect to ( <set 
 9                                         name>/s1,s2 for sets)
10   --port arg                            server port. Can also use --host 
11                                         hostname:port
12   --ipv6                                enable IPv6 support (disabled by 
13                                         default)
14   -u [ --username ] arg                 username
15   -p [ --password ] arg                 password
16   --authenticationDatabase arg          user source (defaults to dbname)
17   --authenticationMechanism arg (=MONGODB-CR)
18                                         authentication mechanism
19   --gssapiServiceName arg (=mongodb)    Service name to use when authenticating
20                                         using GSSAPI/Kerberos
21   --gssapiHostName arg                  Remote host name to use for purpose of 
22                                         GSSAPI/Kerberos authentication
23   --dbpath arg                          directly access mongod database files 
24                                         in the given path, instead of 
25                                         connecting to a mongod  server - needs 
26                                         to lock the data directory, so cannot 
27                                         be used if a mongod is currently 
28                                         accessing the same path
29   --directoryperdb                      each db is in a separate directory 
30                                         (relevant only if dbpath specified)
31   --journal                             enable journaling (relevant only if 
32                                         dbpath specified)
33   -d [ --db ] arg                       database to use
34   -c [ --collection ] arg               collection to use (some commands)
35   -f [ --fields ] arg                   comma separated list of field names 
36                                         e.g. -f name,age
37   --fieldFile arg                       file with field names - 1 per line
38   -q [ --query ] arg                    query filter, as a JSON string, e.g., 
39                                         '{x:{$gt:1}}'
40   --csv                                 export to csv instead of json
41   -o [ --out ] arg                      output file; if not specified, stdout 
42                                         is used
43   --jsonArray                           output to a json array rather than one 
44                                         object per line
45   -k [ --slaveOk ] arg (=1)             use secondaries for export if 
46                                         available, default true
47   --forceTableScan                      force a table scan (do not use 
48                                         $snapshot)
49   --skip arg (=0)                       documents to skip, default 0
50   --limit arg (=0)                      limit the numbers of documents 
51                                         returned, default all
52   --sort arg                            sort order, as a JSON string, e.g., 
53                                         '{x:1}'

2、导入工具-mongoimport

(1)、JSON格式文件导入

/opt/mongodb/bin/mongoimport -h 127.0.0.1 -u user -p password --port 27017 -d dev_test_db -c dev_test_history_location history_location_30487.dat

(2)、CSV格式文件导入

/opt/mongodb/bin/mongoimport -h 127.0.0.1 -u user -p password --port 27017 -d dev_test_db -c dev_test_history_location --type csv --headline --file history_location_30487.csv

参数说明:
-type:    指明要导入的文件格式
-headerline: 指明第一行是列名,不需要导入
-file:    指明要导入的文件

 1 Options:
 2   --help                                produce help message
 3   -v [ --verbose ]                      be more verbose (include multiple times
 4                                         for more verbosity e.g. -vvvvv)
 5   --quiet                               silence all non error diagnostic 
 6                                         messages
 7   --version                             print the program's version and exit
 8   -h [ --host ] arg                     mongo host to connect to ( <set 
 9                                         name>/s1,s2 for sets)
10   --port arg                            server port. Can also use --host 
11                                         hostname:port
12   --ipv6                                enable IPv6 support (disabled by 
13                                         default)
14   -u [ --username ] arg                 username
15   -p [ --password ] arg                 password
16   --authenticationDatabase arg          user source (defaults to dbname)
17   --authenticationMechanism arg (=MONGODB-CR)
18                                         authentication mechanism
19   --gssapiServiceName arg (=mongodb)    Service name to use when authenticating
20                                         using GSSAPI/Kerberos
21   --gssapiHostName arg                  Remote host name to use for purpose of 
22                                         GSSAPI/Kerberos authentication
23   --dbpath arg                          directly access mongod database files 
24                                         in the given path, instead of 
25                                         connecting to a mongod  server - needs 
26                                         to lock the data directory, so cannot 
27                                         be used if a mongod is currently 
28                                         accessing the same path
29   --directoryperdb                      each db is in a separate directory 
30                                         (relevant only if dbpath specified)
31   --journal                             enable journaling (relevant only if 
32                                         dbpath specified)
33   -d [ --db ] arg                       database to use
34   -c [ --collection ] arg               collection to use (some commands)
35   -f [ --fields ] arg                   comma separated list of field names 
36                                         e.g. -f name,age
37   --fieldFile arg                       file with field names - 1 per line
38   --ignoreBlanks                        if given, empty fields in csv and tsv 
39                                         will be ignored
40   --type arg                            type of file to import.  default: json 
41                                         (json,csv,tsv)
42   --file arg                            file to import from; if not specified 
43                                         stdin is used
44   --drop                                drop collection first 
45   --headerline                          first line in input file is a header 
46                                         (CSV and TSV only)
47   --upsert                              insert or update objects that already 
48                                         exist
49   --upsertFields arg                    comma-separated fields for the query 
50                                         part of the upsert. You should make 
51                                         sure this is indexed
52   --stopOnError                         stop importing at first error rather 
53                                         than continuing
54   --jsonArray                           load a json array, not one item per 
55                                         line. Currently limited to 16MB.
原文地址:https://www.cnblogs.com/taoge007/p/5970127.html