如何在MongoDB中将集合导出到CSV? [英] How to export collection to CSV in MongoDB?
问题描述
如何将MongoDB集合中的所有记录导出到.csv
文件?
How do you export all the records in a MongoDB collection to a .csv
file?
mongoexport --host localhost --db dbname --collection name --type=csv > test.csv
这要求我指定我需要导出的字段的名称.我可以仅导出所有字段而无需指定字段名称吗?
This asks me to specify name of the fields I need to export. Can I just export all the fields without specifying the names of fields?
推荐答案
@ karoly-horvath正确. CSV必填字段.
@karoly-horvath has it right. Fields are required for csv.
根据MongoDB问题跟踪器 https://jira.mongodb.org/browse中的错误/SERVER-4224 导出到csv时必须提供字段.文档尚不清楚.这就是错误的原因.
According to this bug in the MongoDB issue tracker https://jira.mongodb.org/browse/SERVER-4224 you MUST provide the fields when exporting to a csv. The docs are not clear on it. That is the reason for the error.
尝试一下:
mongoexport --host localhost --db dbname --collection name --csv --out text.csv --fields firstName,middleName,lastName
更新:
此提交: https://github.com/mongodb/mongo-tools/commit /586c00ef09c32c77907bd20d722049ed23065398 修复了3.0.0-rc10及更高版本的文档.它改变了
This commit: https://github.com/mongodb/mongo-tools/commit/586c00ef09c32c77907bd20d722049ed23065398 fixes the docs for 3.0.0-rc10 and later. It changes
Fields string `long:"fields" short:"f" description:"comma separated list of field names, e.g. -f name,age"`
到
Fields string `long:"fields" short:"f" description:"comma separated list of field names (required for exporting CSV) e.g. -f \"name,age\" "`
3.0及更高版本:
由于已弃用---type=csv
,因此应使用---type=csv
而不是--csv
.
You should use ---type=csv
instead of --csv
since it has been deprecated.
更多详细信息: https://docs. mongodb.com/manual/reference/program/mongoexport/#export-in-csv-format
完整命令:
mongoexport --host localhost --db dbname --collection name --type=csv --out text.csv --fields firstName,middleName,lastName
这篇关于如何在MongoDB中将集合导出到CSV?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!