title: Mongoose.js getter that inflates subdocuments on Find()?
body: <p>Is there an inverse of Mongoose.js validation that can inflate the subdocument when the parent is retrieved?  I may have been looking at the docs so long I'm not recognizing an existing feature for what it is.</p>
  
  <p>A beauty of MongoDB is that the query specifications (e.g. <code>{likes: {$gt: 10, $le: 14}}</code> are themselves Javascript objects, and until recently have been storing them in a MongoDB instance as subdocuments.  </p>
  
  <p>However, upgrading from MongoDB 2.4 to 2.6, these are no longer valid to store as such, and am now getting the error: <code>The dollar ($) prefixed field '$or' ... is not valid for storage</code></p>
  
  <p>Am thus in the situation in this <a href="https://groups.google.com/forum/#!topic/mongodb-user/uViuJsl4jP8" rel="nofollow">Google Groups Discussion</a>.  The author there suggests flattening the document to a String.   This situation can also occur if the subdocuments have legitimate Javascript attributes that have embedded dots (e.g. <code>{"802.11g": ...}</code>)</p>
  
  <p>That's easy enough to by specifying <code>JSON.parse</code> and <code>JSON.stringify</code> as the getter/setter in Mongoose.js:</p>
  
  <pre><code>var ProjectSchema = new Schema({   
    name: { type: String, required: false, default: "New project" },
    spec: {type: mongoose.Schema.Types.Mixed, set: JSON.stringify, get: JSON.parse},
  });
  </code></pre>
  
  <p>But the getter only gets called if I explicitly ask for the attribute value.  The attribute is still a string underneath and gets passed as such:</p>
  
  <pre><code>Project.findById(req.params.projectId, function(err, project) {
    console.log("......"+(typeof project.spec))  // project.spec is an object!
    res.send(project); // project.spec is a String!
  });
  </code></pre>
  
  <p>Obviously i can call <code>model.spec = JSON.parse(model.spec)</code> within each <code>Model.find(...)</code> call and for each flattened attribute but it'd be nice to do it at one central location.</p>
  
  <p><a href="https://groups.google.com/forum/?fromgroups=#!topic/mongoose-orm/8AV6aoJzdiQ" rel="nofollow">https://groups.google.com/forum/?fromgroups=#!topic/mongoose-orm/8AV6aoJzdiQ</a></p>