您的位置:首页 > 数据库 > Mongodb

mongodb-导出数据到csv文件或json文件

2017-07-05 11:25 369 查看
在mongodb的bin目录下, 有一个mongoexport, 可用于数据的导出

[wenbronk@localhost bin]$ ./mongoexport --help
Usage:
mongoexport <options>

Export data from MongoDB in CSV or JSON format.

See http://docs.mongodb.org/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 value, 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') (default: 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
--noHeaderLine                              export CSV data without a list of field names at the first line

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) (default: false)
--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}'
--assertExists                              if specified, export fails if the collection does not exist (default:
false)


导出文件到json中:

导出到json:
$ mongoexport.exe  -d TestDB -c TestCollection -o  ./test.json


导出文件到csv中

./mongoexport --csv -f _id,userId,report_point,matched_line,mode,match_point,predict_point,predict
_lines,poi_ids,is_moved,time,date_time -d mapmatch -c 2017-07-04 -o /opt/wenbronk_soft/2017-07-05.csv


可能会出现乱码, 用记事本另存utf-8 就可以了

在使用-q 查询条件过滤数据的时候,注意两边的 ’ (单引号)

当数据量很大时候,可以使用--skip 配合 --limit,限制单个文件的数据量,方便普通用户查看
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: