Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export valid json from mongodb collection

I am trying to export valid json from a mongodb collection I created using node and instagram's api. I must be missing something as it seems like it should be super simple. I've read other posts and mongo's documentation, specifically about mongoexport.
My end goal is to build a d3 map. I used the basic mongoexport command from the documentation and it returned a json file in the following format:

{'name':'dan'}
{'name':'emma'}

valid json would be:

[{'name':'dan'},
{'name':'emma'}]

I know there are workarounds to this, even one as simple as finding '$' in sublime text which would go to the end of every line, and then could just add a coma. It would just be great to know the proper way technically of doing this. One post suggested using JSON.parse, so I tried this using fs.readFile but it returns the error:

undefined:2
{ "attribution" : null, "tags" : [], "location" : { "latitude" : 48.857141667,
^
SyntaxError: Unexpected token {. 

I simply just need to export the entire mongo collection into a json file that's valid (would successfully pass http://jsonlint.com/ test).

Any help would really be appreciated.

like image 783
Daniel F Avatar asked Apr 18 '14 02:04

Daniel F


People also ask

How do I export a collection from MongoDB?

So, to export data from the MongoDB database, MongoDB provides a command-line tool known as mongoexport. Using this tool you can exports data of a collection in JSON or CSV(comma-separated value) format. Moreover, we can also use features like limit and sort on a collection while exporting the data.


1 Answers

using themongoexport util use --jsonArray

http://docs.mongodb.org/manual/reference/program/mongoexport/#cmdoption--jsonArray

like image 114
David Avatar answered Oct 21 '22 05:10

David