前言
有时候我们需要将把MongoDB中的数据导出为excel CSV文件,我们可以使用mongoexport
命令来完成。所以首先要安装mongodb,关于mongodb的安装这里不做介绍,可以查看官方文档进行安装。
这里只对mongoexport
命令进行介绍,并将MongoDB中的数据导出为CSV文件。
mongoexport 命令的参数
使用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
uri options:
--uri=mongodb-uri mongodb uri connection string
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)
MongoDB中的数据导出为excel CSV文件
从上面的帮助信息可以非常清楚的知道各个命令的含义,所以可以使用下面的命令完成CSV文件的导出:
mongoexport --host=127.0.0.1 --port=27017 -u root -p root1234 -d mydatabase -c user --query='{"age": 18}' --type=csv --fields=_id,name,age,city -o /root/user.csv
在导出数据的过程中遇到如下问题:
too many positional arguments: [18}]
try 'mongoexport --help' for more information
使用提示的命令mongoexport --help
查看帮助信息,很容易就看出问题的原因,我的查询条件没有使用单引号包括起来,所以我们使用一些命令或者操作的时候,遇到问题不要害怕,也不要一遇到问题就去百度,一定要使用帮助命令查看官方文档,这是解决问题最高效的手段,在查看文档的过程中我们也可以学习到更多规范的操作,其实网上很多文章写都是一知半解虽然可以快速解决问题,但是没有官方文档详细和权威。我这里也是做一个简单的总结,方便自己查看,也希望对遇到同样问题的同学提供一点帮助。