title: Node.js Mongoose modifying string data?
body: <p><strong>Abstract</strong></p>
  
  <p>Storing image data as a base64 encoded string works, but when viewing what is in Mongo the original string I saved is only about the first half of the ending persisted string.  Because of this querying does not work.</p>
  
  <p><strong>Details</strong></p>
  
  <p>Consider the following schema:</p>
  
  <pre><code>var mongoose = require('mongoose')
    , Schema = mongoose.Schema;
  
  var imageSchema = new Schema({
      mime:  String,
      bin: String,
      uses : [{type: Schema.Types.ObjectId}]
  });
  
  module.exports = mongoose.model('Image', imageSchema);
  </code></pre>
  
  <p>In the <code>bin</code> field, I am storing base64 image data.  So this string can potentially be large (however this test fails when it is only about 40k as well).</p>
  
  <p><strong>UPDATE</strong> </p>
  
  <p>I have done further analysis and this updated test block shows better the results I am seeing:  </p>
  
  <pre><code>var buffer = getImageFromFileSystem();
  var bin = buffer.toString('base64');
  var i = new Image({
      mime : 'image/jpeg',
      bin : bin
  });
  i.save(function(err, image){
      if (err)
          console.log(err);
      console.log("=== original? "+(image.bin === bin)); //outputs true
      console.log("now find it by id and then compare again")
      Image.findOne({
          _id : image._id
      }, function(err, img) {
          if (err)
              console.log('crap error');
          if (img) {
              console.log('found one!: '+img.id+', lets compare... ');
              console.log("===? "+(img.bin === bin)); //outputs true again!
          }
      });
      //OK so clearly all is well, so let search by the bin str
      Image.findOne({
          bin : bin
      }, function(err, img) {
          if (err)
              console.log('crap error');
          if (!img) {
              console.log("none found") // we hit this... what the heck???
      });
  </code></pre>
  
  <p>So I decide to go look in the database at the <code>bin</code> value.  <em>Here</em> is where I see that its value is my original <code>bin</code> String I saved plus at least twice as many characters appended to the end of it.  So my original base64 string is a substring at the beginning of the value stored in MongoDB.  So when I do <code>findOne({bin:myBinValue})</code> this doesn't work since the comparison is done inside Mongo.  However if I retrieve the <code>bin</code> value from Mongo and then compare, it seems to trim back to my original value and then in my application the javasript comparison is the same!  What gives!?  I have no indexes on this field.  </p>
  
  <pre><code>&gt; db.images.getIndexes()
  [
          {
                  "v" : 1,
                  "key" : {
                          "_id" : 1
                  },
                  "ns" : "mydb.images",
                  "name" : "_id_"
          }
  ]
  </code></pre>