Extension of other answers:
I found @GEverding's answer the most flexible. It also works with aggregation:
test_db.js
print("name,email"); db.users.aggregate([ { $match: {} } ]).forEach(function(user) { print(user.name+","+user.email); } });
Run the following command to export the results:
mongo test_db < ./test_db.js >> ./test_db.csv
Unfortunately, it adds additional text to the CSV file, which requires processing of the file before we can use it:
MongoDB shell version: 3.2.10 connecting to: test_db
But we can make the mongo shell stop splashing out these comments and print only what we requested by passing the --quiet flag
mongo --quiet test_db < ./test_db.js >> ./test_db.csv
Lucky Soni Aug 01 '17 at 12:52 on 2017-08-01 12:52
source share