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!
I had this same issue, and running script in robomongo (Robo 3T 1.1.1) also doesn't allow to copy values and there was no export option either. The best way I could achieve this is to use mongoexport, if mongodb is installed on your local, you can use mongoexport to connect to database on any server and extract data
To connect to Data on remote server, and csv output file, run the following mongoexport in your command line
fieldFile: helps to extract the desired columns, ex: contents of fields.txt can be just:
to only extract values of the column 'userId'
Data on remote server, json output file:
this extracts all fields into the json file
data on localhost (mongodb should be running on localhost)
Reference: https://docs.mongodb.com/manual/reference/program/mongoexport/#use
If you want to use mongoimport, you'll want to export this way:
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:
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.
A Quick and dirty way: Just write your query as
db.getCollection('collection').find({}).toArray()
and right clickCopy JSON
. Paste the data in the editor of your choice.Robomongo's shell functionality will solve the problem. In my case I needed couple of columns as CSV format.