mongodb-导出数据到csv文件或json文件
Posted bronk
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mongodb-导出数据到csv文件或json文件相关的知识,希望对你有一定的参考价值。
在mongodb的bin目录下, 有一个mongoexport, 可用于数据的导出
[[email protected] 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,限制单个文件的数据量,方便普通用户查看
以上是关于mongodb-导出数据到csv文件或json文件的主要内容,如果未能解决你的问题,请参考以下文章