从mongodb集合中导出有效的json [英] Export valid json from mongodb collection

查看:221
本文介绍了从mongodb集合中导出有效的json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从使用node和instagram的api创建的mongodb集合中导出有效的json.我必须丢失一些东西,因为它看起来应该非常简单.我还阅读了其他帖子和mongo的文档,特别是有关mongoexport的文档.
我的最终目标是建立d3地图.我使用了文档中的基本mongoexport命令,它以以下格式返回了json文件:

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'}

有效的json是:

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

我知道有解决方法,即使是简单的解决方法,就像在崇高的文本中找到"$"一样,它会出现在每一行的末尾,然后可能会导致昏迷.最好是从技术上知道正确的方法.一篇文章建议使用JSON.parse,因此我使用fs.readFile尝试了此操作,但它返回错误:

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

我只需要将整个mongo集合导出到有效的json文件中即可(将成功通过 http://jsonlint.com/测试).

任何帮助将不胜感激.

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.

推荐答案

使用themongoexport util使用--jsonArray

using themongoexport util use --jsonArray

http://docs.mongodb.org/manual/参考/程序/mongoexport/#cmdoption--jsonArray

这篇关于从mongodb集合中导出有效的json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆