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!
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..
It works for me.
I don't think robomongo have such a feature. So you better use mongodb function as mongoexport for a specific Collection.
But if you are looking for a backup solution is better to use
Solution:
mongoexport --db test --collection traffic --out traffic.json
where,
database -> mock-server
collection name -> api_defs
output file name -> childChoreRequest.json
Using a robomongo shell script:
Using mongodb's export and import command
You can add the
--jsonArray
parameter / flag to yourmongoexport
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
You can use
tojson
to convert each record to JSON in a MongoDB shell script.Run this script in RoboMongo:
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
.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):
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.
hope this helps someone, as I wish Robo 3T had export capabilities