https://github.com/Studio3T/robomongo/wiki/How-to-export-to-CSV
Created
August 11, 2017 16:59
-
-
Save ConnerAiken/4afd381c2dbbb950ebb30c322a97cc86 to your computer and use it in GitHub Desktop.
Robomongo - add CSV Export option
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Export to CSV function | |
DBQuery.prototype.toCSV = function(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 += headers[index] + ','; | |
} | |
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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment