title: Export text stored as Bindata in mongodb
body: <p>I have a quite big collection of data, made by third parties, it contains a Bindata "package" column where plain ASCII is actually stored.
  I mean, they could have stored the text as string.</p>
  
  <p>I have to export this collection in csv format, which works quite well with mongoexport, but the output contains the base64 encoded values for the "package" column. I need the actual text from that column, not <code>BinData(0,\"NDYuN.....==")</code>.</p>
  
  <p>What I've tried so far is update the collection with a new column "rawData", like this:</p>
  
  <pre><code>db.segments.find({"_id" : ObjectId("4fc79525f65181293930070b")}).forEach(function(data) {
    db.segments.update(
       {_id:data._id},
       {$set:{ "rawData" : data.package.toString() }}
    );
  });
  </code></pre>
  
  <p>I've limited the find to just one document until I get it right.
  Unfortunately toString does not do the magic I expect. </p>
  
  <p>Also, I've tried this:</p>
  
  <pre><code>db.segments.find({"_id" : ObjectId("4fc79525f65181293930070b")}).forEach(function(data){
    data.package = new String(data.package);
    db.segments.save(data); 
  });
  </code></pre>
  
  <p>The result was even worse.</p>
  
  <p>If I read the document with php, <code>$response = $db-&gt;execute('return db.segments.findOne()');</code> then <code>print_r($response)</code> , I can validate that the data is properly stored, as base64.</p>
  
  <p>I couldn't find a solution anywhere, perhaps because nobody ever needed to do something as stupid as this.</p>