MongoDB数据库导⼊和导出脚本
版权声明:本⽂为博主原创⽂章,未经博主允许不得转载。 blog.csdn/QQ994406030/article/details/52303741
⼀、导出⼯具mongoexport
导出
Mongodb中的mongoexport⼯具可以把⼀个collection导出成JSON格式或CSV格式的⽂件。可以通过参数指定导出的数据项,也可以根据指定的条件导出数据。mongoexport具体⽤法如下所⽰:
[root@lb-vm5 bin]# ./mongoexport --help
Usage:  mongoexport <options>
Export data from MongoDB in CSV or JSON format.
See /manual/reference/program/mongoexport/ for more information.
general options:
--help                                      print usage
--version                                  print the tool version and exit
verbosity options:
-v, --verbose=<level>                          more detailed log output (include multiple times for more verbosity, e.g. -vvvvv, or specify a numeric v alue, e.g. --verbose=N)
--quiet                                    hide all log output
connection options:
-h, --host=<hostname>                          mongodb host to connect to (setname/host1,host2 for replica sets)
--port=<port>                              server port (can also use --host hostname:port)
ssl options:
--ssl                                      connect to a mongod or mongos that has ssl enabled
--sslCAFile=<filename>                      the .pem file containing the root certificate chain from the certificate authority
--sslPEMKeyFile=<filename>                  the .pem file containing the certificate and key
--sslPEMKeyPassword=<password>              the password to decrypt the sslPEMKeyFile, if necessary
--sslCRLFile=<filename>                    the .pem file containing the certificate revocation list
--sslAllowInvalidCertificates              bypass the validation for server certificates
--sslAllowInvalidHostnames                  bypass the validation for server name
--sslFIPSMode                              use FIPS mode of the installed openssl library
authentication options:
-u, --username=<username>                      username for authentication
-p, --password=<password>                      password for authentication
--authenticationDatabase=<database-name>    database that holds the user's credentials
--authenticationMechanism=<mechanism>      authentication mechanism to use
namespace options:
-d, --db=<database-name>                        database to use
-c, --collection=<collection-name>              collection to use
output options:
-f, --fields=<field>[,<field>]*                comma separated list of field names (required for exporting CSV) e.g. -f "name,age"
--fieldFile=<filename>                      file with field names - 1 per line
--type=<type>                              the output format, either json or csv (defaults to 'json')
-o, --out=<filename>                            output file; if not specified, stdout is used
--jsonArray                                output to a JSON array rather than one object per line
--pretty                                    output JSON formatted to be human-readable
querying options:
-q, --query=<json>                              query filter, as a JSON string, e.g., '{x:{$gt:1}}'
--queryFile=<filename>                      path to a file containing a query filter (JSON)
-k, --slaveOk                                  allow secondary reads if available (default true)
--readPreference=<string>|<json>            specify either a preference name or a preference json object
--forceTableScan                            force a table scan (do not use $snapshot)
--skip=<count>                              number of documents to skip
--limit=<count>                            limit the number of documents to export
--sort=<json>                              sort order, as a JSON string, e.g. '{x:1}'
1.直接导出数据到⽂件中
./mongoexport -d user_center -c passport -u dev -p 123456-o passport.json
//摩拜单车mongodb查询语句导出以201612开头时间⼩于1482170579000 状态为RENTAL
./bin/mongoexport -d 数据库 -c 集合 -u ⽤户名 -p 密码 -q  '{bikeRentalStatus:"RENTAL",times:{$regex:/201612.*/i},systimes:{ $lt: NumberLong(1 482170579000)}}'  -o 1.jaon
参数说明:
-h:指明数据库宿主机的IP
-u:指明数据库的⽤户名
-p:指明数据库的密码
-d:指明数据库的名字
-c:指明collection的名字
-f:指明要导出那些列
-o:指明到要导出的⽂件名
-q:指明导出数据的过滤条件
-d:指明使⽤的库
-c:指明要导出的集合
-o:指明要导出的⽂件名
从上⾯的结果可以看出,我们在导出数据时没有显⽰指定导出样式 ,默认导出了JSON格式的数据。如果我们需要导出CSV格式的数据,则需要使⽤–csv参数
⼆、导⼊⼯具mongoimport
Mongodb中的mongoimport⼯具可以把⼀个特定格式⽂件中的内容导⼊到指定的collection中。该⼯具可以导⼊JSON格式数据,也可以导⼊CSV格式数据。具体使⽤如下所⽰:./bin/mongoexport -d user_center -c passport -u dev -p ***** -q  '{_id:{$in:[171]}}'  -o 1.jaon
./mongoexport -d user_center -c user_name -u dev -p 123456 --csv -o user_name.json
[root@lb-vm5 bin]# ./mongoimport --help
Usage:  mongoimport <options> <file>
Import CSV, TSV or JSON data into MongoDB. If no file is provided, mongoimport reads from stdin.
See /manual/reference/program/mongoimport/ for more information.
general options:
--help                                      print usage
--version                                  print the tool version and exit
verbosity options:
-v, --verbose=<level>                          more detailed log output (include multiple times for more verbosity, e.g. -vvvvv, or specify a numeric v alue, e.g. --verbose=N)
--quiet                                    hide all log output
connection options:
-h, --host=<hostname>                          mongodb host to connect to (setname/host1,host2 for replica sets)
--port=<port>                              server port (can also use --host hostname:port)
ssl options:
--ssl                                      connect to a mongod or mongos that has ssl enabled
mongodb和mysql结合
--sslCAFile=<filename>                      the .pem file containing the root certificate chain from the certificate authority
--sslPEMKeyFile=<filename>                  the .pem file containing the certificate and key
--sslPEMKeyPassword=<password>              the password to decrypt the sslPEMKeyFile, if necessary
--sslCRLFile=<filename>                    the .pem file containing the certificate revocation list
--sslAllowInvalidCertificates              bypass the validation for server certificates
-
-sslAllowInvalidHostnames                  bypass the validation for server name
--sslFIPSMode                              use FIPS mode of the installed openssl library
authentication options:
-u, --username=<username>                      username for authentication
-p, --password=<password>                      password for authentication
--authenticationDatabase=<database-name>    database that holds the user's credentials
--authenticationMechanism=<mechanism>      authentication mechanism to use
namespace options:
-d, --db=<database-name>                        database to use
-c, --collection=<collection-name>              collection to use
input options:
-
f, --fields=<field>[,<field>]*                comma separated list of field names, e.g. -f name,age
--fieldFile=<filename>                      file with field names - 1 per line
--file=<filename>                          file to import from; if not specified, stdin is used
--headerline                                use first line in input source as the field list (CSV and TSV only)
--jsonArray                                treat input source as a JSON array
--type=<type>                              input format to import: json, csv, or tsv (defaults to 'json')
ingest options:
--drop                                      drop collection before inserting documents
--ignoreBlanks                              ignore fields with empty values in CSV and TSV
--maintainInsertionOrder                    insert documents in the order of their appearance in the input source
-
j, --numInsertionWorkers=<number>              number of insert operations to run concurrently (defaults to 1)
--stopOnError                              stop importing at first insert/upsert error
--upsert                                    insert or update objects that already exist
--upsertFields=<field>[,<field>]*          comma-separated fields for the query part of the upsert
--writeConcern=<write-concern-specifier>    write concern --writeConcern majority, --writeConcern '{w: 3, wtimeout: 500, fsync: t rue, j: true}' (defaults to 'majority')
--bypassDocumentValidation                  bypass document validation
参数说明:
-h:指明数据库宿主机的IP
-u:指明数据库的⽤户名
-p:指明数据库的密码
-d:指明数据库的名字
-c:指明collection的名字
-f:指明要导⼊那些列
-d:指明数据库名
-c:指明collection名
-type:指明要导⼊的⽂件格式
-headerline:指明第⼀⾏是列名,不需要导⼊
-file:指明要导⼊的⽂件
passport.json:导⼊的⽂件名
./mongoimport -d 127.0.0.1 --port 40000 -d user_center -c passport (--type csv) --file passport.json