[MongoDB] Outputting field values containing line breaks in a format that can be imported by Google Spreadsheets

Tadashi Shigeoka ·  Wed, September 17, 2014

I’ll introduce how to output field values containing line breaks from MongoDB in a format that can be imported by Google Spreadsheets.

MongoDB | モンゴディービー

Use , (comma) as the delimiter and enclose fields containing line breaks with ” (double quotes).

In the example below, the detail field contains line breaks.

db.products.find( { detail : regExp } ).forEach(function(p){
  print(p._id.valueOf() + ',"' + p.detail + '"');
});

Then, execute the script and output the results to a file:

mongo --quiet mydb script.js > example.csv

In Google Spreadsheets, you should be able to import without problems using “Delimiter: Detect automatically”.

google-spreadsheet-import

That’s all from the Gemba.