将 Mongo 集合转储为 JSON 格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8991292/
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
Dump Mongo Collection into JSON format
提问by Parvin Gasimzade
Is there any way to dump mongo collection into json format? Either on the shell or using java driver.I am looking for the one with best performance.
有没有办法将 mongo 集合转储为 json 格式?无论是在外壳上还是使用 java 驱动程序。我正在寻找性能最佳的那个。
回答by vrtx
Mongo includes a mongoexport utility (see docs)which can dump a collection. This utility uses the native libmongoclient and is likely the fastest method.
Mongo 包括一个 mongoexport 实用程序(请参阅文档),它可以转储集合。此实用程序使用本机 libmongoclient,可能是最快的方法。
mongoexport -d <database> -c <collection_name>
Also helpful:
也有帮助:
-o: write the output to file, otherwise standard output is used (docs)
-o: 将输出写入文件,否则使用标准输出 ( docs)
--jsonArray: generates a valid json document, instead of one json object per line (docs)
--jsonArray: 生成一个有效的 json 文档,而不是每行一个 json 对象 ( docs)
--pretty: outputs formatted json (docs)
--pretty: 输出格式化的 json ( docs)
回答by Priyanshu Chauhan
Use mongoexport/mongoimport to dump/restore a collection:
使用 mongoexport/mongoimport 转储/恢复集合:
Export JSON File:
导出 JSON 文件:
mongoexport --db <database-name> --collection <collection-name> --out output.json
mongoexport --db <database-name> --collection <collection-name> --out output.json
Import JSON File:
导入 JSON 文件:
mongoimport --db <database-name> --collection <collection-name> --file input.json
mongoimport --db <database-name> --collection <collection-name> --file input.json
WARNING
mongoimportandmongoexportdo not reliably preserve all rich BSON data types because JSON can only represent a subset of the types supported by BSON. As a result, data exported or imported with these tools may lose some measure of fidelity.
警告
mongoimport并mongoexport不能可靠地保留所有丰富的 BSON 数据类型,因为 JSON 只能表示 BSON 支持的类型的一个子集。因此,使用这些工具导出或导入的数据可能会失去一定程度的保真度。
Also, http://bsonspec.org/
BSON is designed to be fast to encode and decode. For example, integers are stored as 32 (or 64) bit integers, so they don't need to be parsed to and from text. This uses more space than JSON for small integers, but is much faster to parse.
In addition to compactness, BSON adds additional data types unavailable in JSON, notably the BinData and Date data types.
BSON 旨在快速编码和解码。例如,整数存储为 32(或 64)位整数,因此它们不需要与文本进行解析。对于小整数,这比 JSON 使用更多的空间,但解析速度要快得多。
除了紧凑性之外,BSON 还添加了 JSON 中不可用的其他数据类型,特别是 BinData 和 Date 数据类型。
回答by nightfury
Here's mine command for reference:
这是我的命令供参考:
mongoexport --db AppDB --collection files --pretty --out output.json
On Windows 7 (MongoDB 3.4), one has to move the cmd to the place where mongod.exeand mongo.exefile resides =>
C:\MongoDB\Server\3.4\binelse it won't work saying it does not recongnize mongoexportcommand.
在Windows 7(MongoDB的3.4),一个具有对CMD移动到的地方mongod.exe和mongo.exe文件所在=>
C:\MongoDB\Server\3.4\bin否则它不会工作,说这不确实recongnizemongoexport命令。
回答by Ninja
From the Mongo documentation:
从 Mongo 文档:
The mongoexport utility takes a collection and exports to either JSON or CSV. You can specify a filter for the query, or a list of fields to output
mongoexport 实用程序获取一个集合并导出到 JSON 或 CSV。您可以为查询指定过滤器,或要输出的字段列表
Read more here: http://www.mongodb.org/display/DOCS/mongoexport
在此处阅读更多信息:http: //www.mongodb.org/display/DOCS/mongoexport
回答by Moe Far
If you want to dump all collections, run this command:
如果要转储所有集合,请运行以下命令:
mongodump -d {DB_NAME} -o /tmp
It will generate all collections data in jsonand bsonextensions into /tmp/{DB_NAME}directory
它将生成目录中的所有集合数据json和bson扩展名/tmp/{DB_NAME}

