47

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.

vcarel
  • 1,787
  • 1
  • 16
  • 23

3 Answers3

65

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
user1063287
  • 10,265
  • 25
  • 122
  • 218
21

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

mongoexport --db myDatabase --collection myCollection --jsonArray --out myJsonFile.json
Ryan
  • 888
  • 1
  • 11
  • 29
Lars
  • 1,006
  • 1
  • 9
  • 29
  • Same, the date format is { "$date" : 1397141347767 } – vcarel Sep 08 '14 at 14:38
  • @vcarel when I do a `db.test.insert({"begin_date" : Date( 1394004372038 ), "foo" : "bar" })` and then `[{ "_id" : { "$oid" : "540bfc4d6b29db6430fcd93e" }, "begin_date" : "Sun Sep 07 2014 08:33:49 GMT+0200 (CEST)", "foo" : "bar" }]` I get: `[{ "_id" : { "$oid" : "540bfc4d6b29db6430fcd93e" }, "begin_date" : "Sun Sep 07 2014 08:33:49 GMT+0200 (CEST)", "foo" : "bar" }]` in test.json – Lars Sep 09 '14 at 09:37
  • Apparently, Date(something) returns the current time as a string. If you try new Date(1394004372038) you'll get in the export "begin_date" : { "$date" : 1394004372038 } – vcarel Sep 09 '14 at 09:53
-2
db.players.find({"name" : "John"}).forEach(printjson);
Krešimir Prcela
  • 4,257
  • 33
  • 46