如何使用人类可读的日期格式将 BSON 转换为 JSON
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23010566/
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
How to convert BSON to JSON with human-readable date format
提问by vcarel
I would like to transform a BSON dump of MongoDB to JSON.
我想将 MongoDB 的 BSON 转储转换为 JSON。
To do that, I'm using the bsondumptool provided with Mongo, but I get an output like :
为此,我使用了Mongo 提供的bsondump工具,但得到的输出如下:
{ "_id" : ObjectId( "5316d194b34f6a0c8776e187" ), "begin_date" : Date( 1394004372038 ), "foo" : "bar" }
{ "_id" : ObjectId( "5316d198b34f6a0c8776e188" ), "begin_date" : Date( 1394004407696 ), "foo" : "bar" }
Can anyone tell me how to get the dates appear in a human readable format (e.g. hh:mm:ss dd/mm/yyyy) ?
谁能告诉我如何让日期以人类可读的格式出现(例如hh:mm:ss dd/mm/yyyy)?
Edit
编辑
It looks like that a more recent version of mongodump outputs dates as:
看起来更新版本的 mongodump 输出日期为:
{ "_id" : ObjectId( "5316d194b34f6a0c8776e187" ), "begin_date" : {"$date":"2015-11-11T08:45:03.974Z"}}, "foo" : "bar" }
So this question is not relevant anymore. Thanks everybody for your help here.
所以这个问题不再相关。在此感谢大家的帮助。
回答by Sanjeev Kumar Rai
bsondump converts BSON files into human-readable formats, including JSON. For example, bsondump is useful for reading the output files generated by mongodump.
bsondump 将 BSON 文件转换为人类可读的格式,包括 JSON。例如,bsondump 可用于读取 mongodump 生成的输出文件。
Source: https://docs.mongodb.com/manual/reference/program/bsondump
来源:https: //docs.mongodb.com/manual/reference/program/bsondump
Examples
例子
bsondump --outFile collection.json collection.bson
The --prettyoption outputs documents in a pretty-printed format JSON, eg:
该--pretty选项以打印精美的 JSON 格式输出文档,例如:
bsondump --pretty --outFile collection.json collection.bson
回答by Lars
To create a JSON file directly from the database, use mongoexport
要直接从数据库创建 JSON 文件,请使用mongoexport
mongoexport --db myDatabase --collection myCollection --jsonArray --out myJsonFile.json

