如何将 mongoDB 数据导出为 CSV 格式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19862137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 13:29:12  来源:igfitidea点击:

How to export mongoDB data into a CSV format?

excelmongodbcsv

提问by dwstein

I'm looking for a very simple way to export data from mongoDB into a CSV. Most of the answers involve bash scripts… etc. Is there a simple mongoDB command that will just export the data into CSV?

我正在寻找一种非常简单的方法将数据从 mongoDB 导出到 CSV。大多数答案都涉及 bash 脚本……等等。是否有一个简单的 mongoDB 命令可以将数据导出到 CSV 中?

回答by andyb

Update:

更新:

As of mongo 3.0.6 --csvis no longer supported and the new flag is --type=csv, so the command would be

从 mongo 3.0.6 开始--csv,不再支持并且新标志是--type=csv,因此命令将是

mongoexport --db users --collection contacts --type=csv --fieldFile fields.txt

mongoexport --db users --collection contacts --type=csv --fieldFile fields.txt

Original answer:

原答案:

This can be done from the command line using the mongo utility functionmongoexport --csv.

这可以使用 mongo实用程序函数从命令行完成mongoexport --csv

Alongside the --csvthe documentation states that you also need to use --fieldsor specify a file with the fields in using --fieldFile.

除了--csv文档说明,您还需要使用--fields或指定带有 using 字段的文件--fieldFile

Have a look at the usage examplesand see if they help, for example:

查看使用示例,看看它们是否有帮助,例如:

mongoexport --db users --collection contacts --csv --fieldFile fields.txt

回答by Fabio Espinosa

If you already have the .bson file, in order to export to csv:

如果您已经有了 .bson 文件,为了导出到 csv:

bsondump collection.bson > file.csv

回答by Jay Dangar

It's pretty easy with mongoDB compass, you just need to goto

使用 mongoDB 指南针很容易,你只需要转到

Collection -> Export Collection -> Select Export data as JSON or CSV

Collection -> Export Collection -> Select Export data as JSON or CSV

and export the data.

并导出数据。