How to convert BSON to JSON with human-readable date format

余生颓废 提交于 2019-12-20 17:38:14

问题


I would like to transform a BSON dump of MongoDB to JSON.

To do that, I'm using the bsondump tool provided with Mongo, but I get an output like :

{ "_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) ?

Edit

It looks like that a more recent version of mongodump outputs dates as:

{ "_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.


回答1:


bsondump converts BSON files into human-readable formats, including JSON. For example, bsondump is useful for reading the output files generated by mongodump.

Source: https://docs.mongodb.com/manual/reference/program/bsondump

Examples

bsondump --outFile collection.json collection.bson

The --pretty option outputs documents in a pretty-printed format JSON, eg:

bsondump --pretty --outFile collection.json collection.bson



回答2:


To create a JSON file directly from the database, use mongoexport

mongoexport --db myDatabase --collection myCollection --jsonArray --out myJsonFile.json


来源:https://stackoverflow.com/questions/23010566/how-to-convert-bson-to-json-with-human-readable-date-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!