106

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 :)

Aafreen Sheikh
  • 4,949
  • 6
  • 33
  • 43
  • 1
    If this is something you do frequently, you might consider writing a standalone EXE using .NET, python, or you could use NodeJs; each has a native driver that would make it easy to execute your code and produce the output desired. – WiredPrairie Jan 23 '13 at 11:37
  • I am referring to Zachary's answer on http://stackoverflow.com/questions/4130849/convert-json-format-to-csv-format-for-ms-excel and am able to convert from json to csv. But as an alternative, can I output the json to a collection and then do a mongoexport? – Aafreen Sheikh Jan 23 '13 at 13:38
  • I'd recommend you just build a small harness using Node and the MongoDB driver for NodeJS and then you can execute whatever code you'd like. You'd get the results you want very quickly without needing the shell at all. It would be very maintainable (and debuggable). – WiredPrairie Jan 23 '13 at 14:44

8 Answers8

202

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.

Zoltán
  • 21,321
  • 14
  • 93
  • 134
GEverding
  • 3,131
  • 2
  • 21
  • 23
  • How would I specify what db the User collection is in? – Nelu Nov 09 '13 at 05:37
  • 2
    @NeluMalancea check out the MongoDB [docs](http://docs.mongodb.org/manual/tutorial/getting-started-with-the-mongo-shell/) they have this information. You can specify the DB by adding `use ` to the top of the script – GEverding Nov 09 '13 at 18:06
  • 2
    Actually, since the shell helpers such as "use " are not javascript, they're not permitted. See http://docs.mongodb.org/manual/tutorial/write-scripts-for-the-mongo-shell/. Instead, start your script something like this: conn = new Mongo(); db = conn.getDB('your_db_name'); – Steve Hansen Smythe Apr 22 '15 at 17:29
  • 2
    @NeluMalancea the mongo command accepts a db url (and user, pass, …) – iwein Jul 09 '15 at 03:48
  • 3
    @NeluMalancea the `test` in the last command **is** the name of the database, just replace it with the name of your database. – Zoltán Jan 18 '16 at 14:42
  • I know this is old but this might help other peoples: You can use a host an a database name like so: `mongo your-host.example.com:27017/dbname` where dbname is your database name and 27017 is the port of the mongod. – amahrt Sep 05 '16 at 10:52
  • I had a textual field with lots of nasty user-input values with quotes and newlines, so I also defined a [string escaping function like this one](https://github.com/joliss/js-string-escape/blob/master/index.js) within my export script. It works, but I can't help feeling this is all getting very heavy. If MongoDB doesn't do this a more built-in way, then maybe saving as JSON and converting that file to CSV afterwards was a better idea. – Harry Wood Sep 21 '16 at 14:29
131

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
Juanjo Martinez
  • 679
  • 7
  • 18
thisarattr
  • 1,938
  • 1
  • 13
  • 11
  • 18
    Thanks a ton! Hint: now it is `--type=csv` instead of `--csv`. – Jan Feb 11 '16 at 13:44
  • The limitation of the mongoexport is that you can't manipulate the fields. The mongo id exports as ObjectId(mongidstring). Being able to export the results from a mongo shell script is better if someone wants to manipulate the data of the fields (for example ObjectId(mongidstring).toString()). – Raj006 Dec 05 '18 at 15:11
  • 2
    can I do aggregation operations? – Hendy Irawan Jun 24 '19 at 07:25
  • 1
    This solution worked. But for Windows I had to make two amendments: I just needed double apostrophe from outside and single apostrophes inside like this -q "{name:'stackoverflow'}" , also for port specifying -p command didn't work, I used --port 27000. – nurb Feb 13 '20 at 10:19
  • The Windows zip containing mongoexport is flagged by Virustotal as having Trojan-PSW.Agent - so not going to use that command personally. – JGFMK Apr 20 '21 at 09:36
  • Thanks @thisarattr. I explored with `mongoexport --help` command. If anyone needs explanation to the command shared, you can take a look at below syntax: `mongoexport --host=yourHost --port=yourPort --db=yourDbName --collection=yourCollectionName --type=csv --fields=CommaSeparatedFields --query='YourQuery' --out=outputFile.csv` – Preet Sep 16 '22 at 15:01
13

Extending other answers:

I found @GEverding's answer 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);
    }
});

Execute the following command to export results:

mongo test_db < ./test_db.js >> ./test_db.csv

Unfortunately, it adds additional text to the CSV file which requires processing the file before we can use it:

MongoDB shell version: 3.2.10 
connecting to: test_db

But we can make mongo shell stop spitting out those comments and only print what we have asked for by passing the --quiet flag

mongo --quiet test_db < ./test_db.js >> ./test_db.csv
Lucky Soni
  • 6,811
  • 3
  • 38
  • 57
6

Here is what you can try:

print("id,name,startDate")
cursor = db.<collection_name>.find();
while (cursor.hasNext()) {
    jsonObject = cursor.next();
    print(jsonObject._id.valueOf() + "," + jsonObject.name + ",\"" + jsonObject.stateDate.toUTCString() +"\"")

}

Save that in a file, say "export.js". Run the following command:

mongo <host>/<dbname> -u <username> -p <password> export.js > out.csv
Shirish Kumar
  • 1,532
  • 17
  • 23
5

Have a look at this

for outputing from mongo shell to file. There is no support for outputing csv from mongos shell. You would have to write the javascript yourself or use one of the many converters available. Google "convert json to csv" for example.

Community
  • 1
  • 1
geakie
  • 1,458
  • 9
  • 9
2

Just weighing in here with a nice solution I have been using. This is similar to Lucky Soni's solution above in that it supports aggregation, but doesn't require hard coding of the field names.

cursor = db.<collection_name>.<my_query_with_aggregation>;

headerPrinted = false;
while (cursor.hasNext()) {
    item = cursor.next();
    
    if (!headerPrinted) {
        print(Object.keys(item).join(','));
        headerPrinted = true;
    }

    line = Object
        .keys(item)
        .map(function(prop) {
            return '"' + item[prop] + '"';
        })
        .join(',');
    print(line);
}

Save this as a .js file, in this case we'll call it example.js and run it with the mongo command line like so:

mongo <database_name> example.js --quiet > example.csv
Dharman
  • 30,962
  • 25
  • 85
  • 135
TimmyGee
  • 268
  • 2
  • 5
0

I use the following technique. It makes it easy to keep the column names in sync with the content:

var cursor = db.getCollection('Employees.Details').find({})

var header = []
var rows = []

var firstRow = true
cursor.forEach((doc) => 
{
    var cells = []
    
    if (firstRow) header.push("employee_number")
    cells.push(doc.EmpNum.valueOf())

    if (firstRow) header.push("name")
    cells.push(doc.FullName.valueOf())    

    if (firstRow) header.push("dob")
    cells.push(doc.DateOfBirth.valueOf())   
    
    row = cells.join(',')
    rows.push(row)    

    firstRow =  false
})

print(header.join(','))
print(rows.join('\n'))
Fidel
  • 7,027
  • 11
  • 57
  • 81
0

When executing a script in a remote server. Mongo will add its own logging output, which we might want to omit from our file. --quiet option will only disable connection related logs. Not all mongo logs. In such case we might need to filter out unneeded lines manually. A Windows based example:

mongo dbname --username userName --password password --host replicaset/ip:port --quiet printDataToCsv.js | findstr /v "NETWORK" > data.csv

This will pipe the script output and use findstr to filter out any lines, which have NETWORK string in them. More information on findstr: https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/findstr

A Linux version of this would use grep.

Simon Katanski
  • 426
  • 5
  • 8