如何使用Robomongo从MongoDB导出JSON

问题描述:

所以我对MongoDB不太了解.我使用RoboMongo来连接到MongoDB.我需要做的是-该MongoDB中有一个集合.我想从该集合中导出数据,以便将其保存到文件中.

So I do not know much about MongoDB. I have RoboMongo using which I connect to a MongoDB. What I need to do is this - there is a collection in that MongoDB. I want to export the data from that collection so that I can save it into a file.

我使用该界面以文本形式打开集合中的数据,并进行了 Ctrl + A 并粘贴到文本文件中.但是,我发现并非所有数据都被复制,而且文本数据中有很多注释自然会破坏JSON.

I used the interface to open the data from the collection as text and did a Ctrl + A and pasted into a text file. However, I found that not all data is copied and also that there were many comments in the text data which naturally breaks the JSON.

我想知道RoboMongo是否具有Export As JSON功能,以便我可以进行干净导出.

I am wondering if RoboMongo has a Export As JSON facility so that I can do a clean export.

任何指针都值得赞赏!

您可以使用tojson在RoboMongo中运行此脚本:

Run this script in RoboMongo:

var cursor = db.getCollection('foo').find({}, {});
while(cursor.hasNext()) {
    print(tojson(cursor.next()))
}

这会将所有结果打印为类似JSON的数组.

This prints all results as a JSON-like array.

结果不是真正的JSON!日期和对象ID之类的某些类型会作为JavaScript函数调用输出,例如ISODate("2016-03-03T12:15:49.996Z").

The result is not really JSON! Some types, such as dates and object IDs, are printed as JavaScript function calls, e.g., ISODate("2016-03-03T12:15:49.996Z").

对于大型结果集可能不是很有效,但是您可以限制查询.或者,您可以使用 mongoexport .

Might not be very efficient for large result sets, but you can limit the query. Alternatively, you can use mongoexport.