如何使用 Robomongo 从 MongoDB 导出 JSON
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28733692/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to export JSON from MongoDB using Robomongo
提问by Undefined Variable
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.
所以我不太了解MongoDB
。我RoboMongo
使用它连接到 MongoDB。我需要做的是 - 在那个 MongoDB 中有一个集合。我想从该集合中导出数据,以便将其保存到文件中。
I used the interface to open the data from the collection as text and did a Ctrl+ Aand 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.
我使用该界面将集合中的数据作为文本打开,然后执行Ctrl+A并粘贴到文本文件中。然而,我发现并不是所有的数据都被复制了,而且文本数据中有很多注释自然会破坏 JSON。
I am wondering if RoboMongo has a Export As JSON
facility so that I can do a clean export.
我想知道 RoboMongo 是否有Export As JSON
设施以便我可以进行干净的出口。
Any pointers are appreciated!
任何指针表示赞赏!
回答by Florian Winter
You can use tojson
to convert each record to JSON in a MongoDB shell script.
您可以使用MongoDB shell 脚本tojson
将每条记录转换为 JSON 。
Run this script in RoboMongo:
在 RoboMongo 中运行此脚本:
var cursor = db.getCollection('foo').find({}, {});
while(cursor.hasNext()) {
print(tojson(cursor.next()))
}
This prints all results as a JSON-like array.
这会将所有结果打印为类似 JSON 的数组。
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")
.
结果并不是真正的 JSON!某些类型,例如日期和对象 ID,会打印为 JavaScript 函数调用,例如ISODate("2016-03-03T12:15:49.996Z")
.
Might not be very efficient for large result sets, but you can limit the query. Alternatively, you can use mongoexport
.
对于大型结果集可能不是很有效,但您可以限制查询。或者,您可以使用mongoexport
.
回答by ?bl
回答by Anish Abraham
Robomongo's shell functionality will solve the problem. In my case I needed couple of columns as CSV format.
Robomongo 的 shell 功能将解决这个问题。在我的情况下,我需要几列作为 CSV 格式。
var cursor = db.getCollection('Member_details').find({Category: 'CUST'},{CustomerId :1,Name :1,_id:0})
while (cursor.hasNext()) {
var record = cursor.next();
print(record.CustomerID + "," + record.Name)
}
Output : -------
334, Harison
433, Rechard
453, Michel
533, Pal
回答by Tomek
There are a few MongoDB GUIs out there, some of them have built-in support for data exporting. You'll find a comprehensive list of MongoDB GUIs at http://mongodb-tools.com
有一些 MongoDB GUI,其中一些具有对数据导出的内置支持。您可以在http://mongodb-tools.com 上找到 MongoDB GUI 的完整列表
You've asked about exporting the results of your query, and not about exporting entire collections. Give 3T MongoChef MongoDB GUIa try, this tool has support for your specific use case.
您询问的是导出查询结果,而不是导出整个集合。给3T MongoChef MongoDB的GUI一个尝试,这个工具对您的具体使用情况的支持。
回答by russiansummer
you say "export to file" as in a spreadsheet? like to a .csv?
你在电子表格中说“导出到文件”?喜欢.csv?
IMO this is the EASIEST way to do this in Robo 3T (formerly robomongo):
IMO 这是在 Robo 3T(以前称为 robomongo)中执行此操作的最简单方法:
In the top right of the Robo 3T GUI there is a "View Results in text mode" button, click it and copy everything
paste everything into this website: https://json-csv.com/
click the download button and now you have it in a spreadsheet.
在 Robo 3T GUI 的右上角有一个“以文本模式查看结果”按钮,单击它并复制所有内容
将所有内容粘贴到此网站:https: //json-csv.com/
单击下载按钮,现在您已将其保存在电子表格中。
hope this helps someone, as I wish Robo 3T had export capabilities
希望这对某人有所帮助,因为我希望 Robo 3T 具有导出功能
回答by Yogesh Nikam Patil
Don't run this command on shell, enter this script at a command prompt with your database name, collection name, and file name, all replacing the placeholders..
不要在 shell 上运行此命令,在命令提示符下输入此脚本,并使用您的数据库名称、集合名称和文件名,所有这些都替换占位符。
mongoexport --db (Database name) --collection (Collection Name) --out (File name).json
It works for me.
这个对我有用。
回答by henrily
I don't think robomongo have such a feature. So you better use mongodb function as mongoexport for a specific Collection.
我不认为 robomongo 有这样的功能。因此,您最好将 mongodb 函数用作特定集合的 mongoexport。
http://docs.mongodb.org/manual/reference/program/mongoexport/#export-in-json-format
http://docs.mongodb.org/manual/reference/program/mongoexport/#export-in-json-format
But if you are looking for a backup solution is better to use
但是,如果您正在寻找备份解决方案,则最好使用
mongodump / mongorestore
回答by Mark Shust at M.academy
Expanding on Anish's answer, I wanted something I can apply to any query to automatically output all fields vs. having to define them within the print statement. It can probably be simplified but this was something quick & dirty that works great:
扩展 Anish 的答案,我想要一些可以应用于任何查询的东西,以自动输出所有字段,而不是必须在打印语句中定义它们。它可能可以简化,但这是一种快速而肮脏的东西,效果很好:
var cursor = db.getCollection('foo').find({}, {bar: 1, baz: 1, created_at: 1, updated_at: 1}).sort({created_at: -1, updated_at: -1});
while (cursor.hasNext()) {
var record = cursor.next();
var output = "";
for (var i in record) {
output += record[i] + ",";
};
output = output.substring(0, output.length - 1);
print(output);
}
回答by shapiromatron
If you want to use mongoimport, you'll want to export this way:
如果您想使用mongoimport,您需要以这种方式导出:
db.getCollection('tables')
.find({_id: 'q3hrnnoKu2mnCL7kE'})
.forEach(function(x){printjsononeline(x)});
回答by Herald Smit
Using a robomongo shell script:
使用 robomongo shell 脚本:
//on the same db
var cursor = db.collectionname.find();
while (cursor.hasNext()) {
var record = cursor.next();
db.new_collectionname.save(record);
}
Using mongodb's export and import command
使用mongodb的export和import命令
You can add the --jsonArray
parameter / flag to your mongoexport
command, this exports the result as single json array.
您可以将--jsonArray
参数/标志添加到您的mongoexport
命令中,这会将结果导出为单个 json 数组。
Then just specify the --jsonArray
flag again when importing.
然后--jsonArray
在导入时再次指定标志。
Or remove the starting and ending array brackets [] in the file, then your modified & exported file will import with the mongoimport
command without the --jsonArray
flag.
或者删除文件中的开始和结束数组括号 [],然后您修改和导出的文件将使用mongoimport
没有--jsonArray
标志的命令导入。
More on Export here: https://docs.mongodb.org/manual/reference/program/mongoexport/#cmdoption--jsonArray
更多关于导出在这里:https: //docs.mongodb.org/manual/reference/program/mongoexport/#cmdoption--jsonArray
Import here: https://docs.mongodb.org/manual/reference/program/mongoimport/#cmdoption--jsonArray
在此处导入:https: //docs.mongodb.org/manual/reference/program/mongoimport/#cmdoption--jsonArray