title: How do you find by a sub-document reference using mongoose?
body: <p>I'm trying to search off the contents of a sub-document referenced by ObjectId, like so:</p>
  
  <pre><code>Model.Idea.findOne('_key.code':code).populate('_key').exec (err,idea) -&gt;
    return done err if err
    should.exist idea
  </code></pre>
  
  <p>The schema looks like this:</p>
  
  <pre><code>Key = new mongoose.Schema(
    type:
      type: String
      enum: types
      index: true
    code:
      type: String
      index:
        unique: true
  )
  Model.Key = ...
  
  Idea = new mongoose.Schema(
    text: String
    name: String
    _key:
      type: mongoose.Schema.Types.ObjectId
      ref: 'Key'
  )
  Model.Idea = ...
  </code></pre>
  
  <p>The reason that I'm doing it like this is that I want to pre-allocate a bunch of keys across a variety of key types and then allocated them out as needed.</p>
  
  <p>For some reason I thought this was possible but the query doesn't seem to be returning any results. Is there a way to do it without looking up the key first and then referencing the Id? I think I may have got the impression you can do this from embedded subdocuments though...</p>