Created
April 6, 2017 13:55
-
-
Save solocommand/11722930a1b843da6091bb57cf6d7961 to your computer and use it in GitHub Desktop.
MongoDB Server-side CSV formatting
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
db.system.js.save({ | |
_id: "csv", | |
value: function(doc, fields) { | |
var getValue = function(doc, field) { | |
var tree = field.split('.'); | |
var value = doc; | |
tree.forEach(function(key) { | |
value = value[key]; | |
}) | |
return value; | |
}; | |
var str = "\"" + doc._id + "\""; | |
fields.forEach(function(field) { | |
str = str + ",\"" + getValue(doc, field) + "\""; | |
}) | |
return str; | |
} | |
}); | |
db.loadServerScripts(); | |
var fields = ['email', 'displayName', 'username', 'geoCode.ip', 'geoCode.hostname']; | |
db.getCollection('Customer').find({"geoCode.hostname":/tor/}).forEach(function(doc) { | |
print(csv(doc, fields)) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment