Last active
September 21, 2021 10:41
-
-
Save axelvnk/b3e5f7d4c48eba0975feca06248dfce4 to your computer and use it in GitHub Desktop.
Export Mongo query result to CSV
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.getCollection('event').find({whatever_field: 'value'}).forEach(function(u) { | |
print('"' + u._id + '";"' + u.createdAt.toJSON() + '";'); | |
}); |
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
mongo db_name --username db_user --password db_password export.js > export.csv | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Simply create the javascript file and put it somewhere on your server. Next, execute the mongo command. Javascript will loop over the results and output every line to a csv file. That's it.