Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect output of mongo query to a csv file

I am using MongoDB 2.2.2 for 32-bit Windows7 machine. I have a complex aggregation query in a .js file. I need to execute this file on the shell and direct the output to a CSV file. I ensure that the query returns a "flat" json (no nested keys), so it is inherently convertible to a neat csv.

I know about load() and eval(). eval() requires me to paste the whole query into the shell and allows only printjson() inside the script, while I need csv. And, the second way: load()..It prints the output on the screen, and again in json format.

Is there a way Mongo can do this conversion from json to csv? (I need csv file to prepare charts on the data). I am thinking:

1. Either mongo has a built-in command for this that I can't find right now.
2. Mongo can't do it for me; I can at most send the json output to a file which I then need to convert to csv myself.
3. Mongo can send the json output to a temporary collection, the contents of which can be easily mongoexported to csv format. But I think only map-reduce queries support output collections. Is that right? I need it for an aggregation query.

Thanks for any help :)

like image 961
Aafreen Sheikh Avatar asked Jan 23 '13 11:01

Aafreen Sheikh


2 Answers

I know this question is old but I spend an hour trying to export a complex query to csv and I wanted to share my thoughts. First I couldn't get any of the json to csv converters to work (although this one looked promising). What I ended up doing was manually writing the csv file in my mongo script.

This is a simple version but essentially what I did:

print("name,id,email"); db.User.find().forEach(function(user){   print(user.name+","+user._id.valueOf()+","+user.email); }); 

This I just piped the query to stdout

mongo test export.js > out.csv 

where test is the name of the database I use.

like image 57
GEverding Avatar answered Sep 22 '22 21:09

GEverding


Mongo's in-built export is working fine, unless you want to any data manipulation like format date, covert data types etc.

Following command works as charm.

    mongoexport -h localhost -d databse -c collection --type=csv      --fields erpNum,orderId,time,status      -q '{"time":{"$gt":1438275600000}, "status":{"$ne" :"Cancelled"}}'      --out report.csv 
like image 26
thisarattr Avatar answered Sep 23 '22 21:09

thisarattr