How to export JSON from MongoDB using Robomongo
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.
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