How to export JSON from MongoDB using Robomongo

217,677

Solution 1

A Quick and dirty way: Just write your query as db.getCollection('collection').find({}).toArray() and right click Copy JSON. Paste the data in the editor of your choice.

enter image description here

Solution 2

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.

Solution 3

Robomongo's shell functionality will solve the problem. In my case I needed couple of columns as CSV format.

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

Solution 4

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

Solution 5

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

You've asked about exporting the results of your query, and not about exporting entire collections. Give 3T MongoChef MongoDB GUI a try, this tool has support for your specific use case.

Share:
217,677
Undefined Variable
Author by

Undefined Variable

Updated on August 10, 2021

Comments

  • Undefined Variable
    Undefined Variable over 2 years

    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!

    • Ramesh Murugesan
      Ramesh Murugesan about 9 years
      You want to export particular collections or full db?
    • Stennie
      Stennie about 9 years
      This isn't a current feature of Robomongo, but I've added a feature suggestion in the github issue queue: Add JSON export. There's a general suggestion that import/export should be integrated, but more detailed/practical use cases would be helpful. For example, should this support exporting JSON from a collection, a find query, an aggregation pipeline? Currently your best option is to use the standard mongoexport command line tool.
    • Undefined Variable
      Undefined Variable about 9 years
      @Stennie - thank you for your comment. I guess in answer to your question - from a customer experience viewpoint, it does not really matter. In most other DB interfaces, the flow is that you run a query (with or without critera), get a set of results. Right click and say "export results as..." So the same should be applicable here. Does not matter if I am exporting an entire collection or a find query. If the result can get displayed in the panel, then it should be exportable.
    • Naren
      Naren almost 4 years
      Simply you can do this mongoexport --uri='mongodb://[email protected]:27017/marketi‌​ng' --collection=contacts --out=contacts.json
    • Szczepan Hołyszewski
      Szczepan Hołyszewski about 2 years
      @Naren Right click -> "Export as JSON" would be simple. This is not.
  • chopin_is_the_best
    chopin_is_the_best over 7 years
    Is it possible to write the output of this script into a csv locally whithin Robomongo shell ?
  • ruX
    ruX over 7 years
    It outputs not valid json. Just json-serialized records one by one
  • vinyll
    vinyll about 6 years
    Studio 3T totally did the job easier than expected! :+1:
  • Yuval
    Yuval over 5 years
    For many use-cases, one can use tojson(db.getCollection(...).find(...)["_batch"]) to output the entirety of the current batch obtained from the server.
  • Florian Winter
    Florian Winter over 5 years
    @Yuval You mean literally ["_batch"]? Can you give an example how to use this? I tried this with Robo 3T 1.2.1, but it only says "Script executed successfully, but there are no results to show".
  • Yuval
    Yuval over 5 years
    @FlorianWinter That was just from random findings from fiddling around. A better solution is tojson(db.getCollection(...).find(...).toArray()).
  • Florian Winter
    Florian Winter over 5 years
    @Yuval Nice! That's the easiest solution then, much better than mine. Consider posting it as an answer. (Or edit mine, but then I would get all the credit that you deserve, which would be somewhat unfair...)
  • Florian Winter
    Florian Winter over 5 years
    @Yuval A downside of your approach may be that the entire collection is loaded into memory, then converted to JSON. Or that it only outputs part of the collection. I did not test it thoroughly. Mine outputs the whole collection and only keeps one document in memory per iteration.
  • Yuval
    Yuval over 5 years
    Your last statement is incorrect: your approach appends each record to the variable records hence all records are kept in memory. There is nothing much to test, it's just more concise code I thought I would share.
  • Florian Winter
    Florian Winter over 5 years
    @Yuval Thanks for the heads-up. I have improved the script.
  • Shanika Ediriweera
    Shanika Ediriweera over 4 years
    This gives me "Script executed successfully, but there are no results to show"
  • Shanika Ediriweera
    Shanika Ediriweera over 4 years
    This gives me "Script executed successfully, but there are no results to show"
  • EugenSunic
    EugenSunic over 4 years
    Error: Line 10: Invalid left-hand side in assignment
  • Priya Agarwal
    Priya Agarwal about 4 years
    @Florian Winter - Follow up question on your Note : How to convert BinData and ISODate type functions to actual values(the way it is represented in GUI) while export?
  • Florian Winter
    Florian Winter about 4 years
    @PriyaAgarwal I'm not sure I fully understand your question, but if it is a follow-up question, then I would suggest asking it as a new question. You will be more likely to get help on here if you do so.
  • Constantino Cronemberger
    Constantino Cronemberger about 4 years
    When I have dates in my document it returns ISODate elements which are not valid json format.
  • Ilya Luzyanin
    Ilya Luzyanin about 4 years
    Neat! Not dirty at all considering lack of export functionality in robo3t. Much easier for small sets of data comparing to other suggested solutions.
  • maesk
    maesk almost 4 years
    Why would you use a Word document to store a database export? And why in the world would anyone want to print a database export?
  • Charlie Schliesser
    Charlie Schliesser almost 4 years
    @maesk 🤣🤣🤣🤣
  • Visakh Vijayan
    Visakh Vijayan over 3 years
    awesome. just what I was looking for
  • Shurvir Mori
    Shurvir Mori over 3 years
    Awesome, It helps me lot, Just a little expectation, can we export whole collection in single time. this answer helps to export single single tables.
  • realtebo
    realtebo about 3 years
    Studio 3T is perfect but it has an excesive cost for a solo developer like me. Absurd the cost.
  • craastad
    craastad about 3 years
    This actually helped solve my problem while my IntelliJ data connection is broken. Sad I have to jump through hoops like this in the year 2021, but hey, it works. Thank you!
  • emragins
    emragins almost 3 years
    This was the best way to get more than 50 records at a time. I just switched the print to print(record) and then was able to copy the json from the text view.
  • jcollum
    jcollum almost 3 years
    The dates will come out as ISODate instead of 8601 JSON dates though
  • Yuki
    Yuki over 2 years
    Awesome! Just a reminder - don't run it on large collections or result sets :-)
  • santamanno
    santamanno over 2 years
    This will also include comments between the objects and you need to manually insert a [ at the beginning and a ] at the end. Also, you need to replace ObjectID and ISODate functions.
  • cem
    cem over 2 years
    For medium or small results sets, its really great! Thanks.
  • Prabhatika Vij
    Prabhatika Vij about 2 years
    Just to add to @Yuki's comment, you might get a similar error if you try to run it on large collections: Error: Converting from JavaScript to BSON failed: Object size 52590654 exceeds limit of 16793600 bytes. : shellPrintHelper@src/mongo/shell/utils.js:637:9 @(shell2):1:35.