mirror of
https://github.com/Studio3T/robomongo.git
synced 2025-12-04 16:25:19 -05:00
Page:
How to export to CSV
Pages
Build Robomongo (0.8.x 0.9.0 RC9)
Feature Spec
Home
How Welcome Tab works
How to export to CSV
How to use SSH port forwarding with Robo 3T
New Release Check List
Notes: Upgrade to MongoDB 4.0
Package Robomongo
Qt Build System
Restore connection strings from versions below 0.9.0 RC2
Robo 3T Schematics: Build, Class and UI Diagrams
Robomongo Code Quality
Robomongo Code Review
Robomongo Coding Style
Robomongo Config File Guide
Robomongo Config File
Robomongo Cplusplus 11, 14 Transition Guide
Robomongo ECMAScript 2015 (aka ES6) Support
Static Code Analysis
Tests
Translator's guide
Unit Tests
Upgrade Guide From MongoDB 3.2 to 3.4 and 3.4 to 4.0
Upgrade Guide to MongoDB 4.2 from 4.0
macOS Upgrade Guide
No results
3
How to export to CSV
avimar edited this page 2019-08-19 21:25:34 +03:00
Table of Contents
Export/Import to/from JSON and CSV features are under development.
Until these features are available, here is workaround solution how to export to CSV using Robomongo shell:
Workaround Solution: How to export to CSV
1) Create .robomongorc.js file with the following function into your home directory.
(Note: Specials thanks to @jeff-phil for this function: https://github.com/Studio3T/robomongo/issues/348#issuecomment-29362405 and @Ian Newson for the way to do it for aggregates https://stackoverflow.com/questions/16468602/mongoexport-aggregate-export-to-a-csv-file/48928948#48928948 )
// Export to CSV function
function toCSV (deliminator, textQualifier)
{
var count = -1;
var headers = [];
var data = {};
var cursor = this;
deliminator = deliminator == null ? ',' : deliminator;
textQualifier = textQualifier == null ? '\"' : textQualifier;
while (cursor.hasNext()) {
var array = new Array(cursor.next());
count++;
for (var index in array[0]) {
if (headers.indexOf(index) == -1) {
headers.push(index);
}
}
for (var i = 0; i < array.length; i++) {
for (var index in array[i]) {
data[count + '_' + index] = array[i][index];
}
}
}
var line = '';
for (var index in headers) {
line += textQualifier + headers[index] + textQualifier + deliminator;
}
line = line.slice(0, -1);
print(line);
for (var i = 0; i < count + 1; i++) {
var line = '';
var cell = '';
for (var j = 0; j < headers.length; j++) {
cell = data[i + '_' + headers[j]];
if (cell == undefined) cell = '';
line += textQualifier + cell + textQualifier + deliminator;
}
line = line.slice(0, -1);
print(line);
}
}
DBQuery.prototype.toCSV=toCSV; //regular find
DBCommandCursor.prototype.toCSV=toCSV; //aggregates
2) Close/open Robomongo to load the script.
3) Example usage:
db.getCollection('csv_coll').find({}).toCSV()
Output on Robomongo shell:
_id,name,surname,age
"584ac96e93775e9ffab70f52","alex","desouza","4"
"58d21b2a8a7c03ab0860fd7d","john","wick","40"
"58d21b378a7c03ab0860fd7e","mike","costanza","44"