How to export json from Mongodb using robomongo

2019-03-08 04:54发布

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.

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.

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

Any pointers are appreciated!

12条回答
对你真心纯属浪费
2楼-- · 2019-03-08 05:25

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

mongoexport --db (Database name) --collection (Collection Name) --out (File name).json

It works for me.

查看更多
等我变得足够好
3楼-- · 2019-03-08 05:26

I don't think robomongo have such a feature. So you better use mongodb function as mongoexport for a specific Collection.

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
查看更多
戒情不戒烟
4楼-- · 2019-03-08 05:28

Solution:
mongoexport --db test --collection traffic --out traffic.json

enter image description here


where,
database -> mock-server
collection name -> api_defs
output file name -> childChoreRequest.json

查看更多
Root(大扎)
5楼-- · 2019-03-08 05:30

Using a robomongo shell script:

//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

You can add the --jsonArray parameter / flag to your mongoexport command, this exports the result as single json array.

Then just specify the --jsonArray flag again when importing.

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.

More on Export here: https://docs.mongodb.org/manual/reference/program/mongoexport/#cmdoption--jsonArray

Import here: https://docs.mongodb.org/manual/reference/program/mongoimport/#cmdoption--jsonArray

查看更多
贼婆χ
6楼-- · 2019-03-08 05:33

You can use tojson to convert each record to JSON in a MongoDB shell script.

Run this script in RoboMongo:

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

This prints all results as a JSON-like array.

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").

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

查看更多
Viruses.
7楼-- · 2019-03-08 05:33

you say "export to file" as in a spreadsheet? like to a .csv?

IMO this is the EASIEST way to do this in Robo 3T (formerly robomongo):

  1. In the top right of the Robo 3T GUI there is a "View Results in text mode" button, click it and copy everything

  2. paste everything into this website: https://json-csv.com/

  3. click the download button and now you have it in a spreadsheet.

hope this helps someone, as I wish Robo 3T had export capabilities

查看更多
登录 后发表回答