Skip to content

Instantly share code, notes, and snippets.

@axelvnk
Last active September 21, 2021 10:41
Show Gist options
  • Save axelvnk/b3e5f7d4c48eba0975feca06248dfce4 to your computer and use it in GitHub Desktop.
Save axelvnk/b3e5f7d4c48eba0975feca06248dfce4 to your computer and use it in GitHub Desktop.
Export Mongo query result to CSV
db.getCollection('event').find({whatever_field: 'value'}).forEach(function(u) {
print('"' + u._id + '";"' + u.createdAt.toJSON() + '";');
});
mongo db_name --username db_user --password db_password export.js > export.csv
@axelvnk
Copy link
Author

axelvnk commented Oct 30, 2017

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment