Reference item in sub-schema of model in Mongoose ODM
<p>Im setting up my MongoDB models, and I have one models schema (<code>Partition</code> model) setup in such a way that one of the schema items (<code>fields</code>) is an array of items that follows another schema (<code>Field</code> schema)</p>

<p>Here's the Partition model (with the Partition schema and the Field schema):</p>

<pre class="lang-js prettyprint-override"><code>// Partition model
module.exports = Mongoose =&gt; {     
    const Schema = Mongoose.Schema

    // Field Schema
    const fieldSchema = new Schema({
        name: {
            type: Schema.Types.String
        }
    })

    // Partition Schema
    const partitionSchema = new Schema({
        name: {
            type: Schema.Types.String
        },
        // `fields` is an array of objects that must follow the `fieldSchema`
        fields: [ fieldSchema ]
    })

    return Mongoose.model( 'Partition', partitionSchema )
}
</code></pre>

<p>Then I have another model (<code>Asset</code> model), which has an <code>attributes</code> array, which holds objects that each have two items, <code>_field</code> and <code>value</code>. the <code>_field</code> needs to be an ID that will reference an item in the Partition models <code>fields._id</code> values.</p>

<p>Heres the Asset model:</p>

<pre class="lang-js prettyprint-override"><code>// Asset model
module.exports = Mongoose =&gt; {     
    const Schema = Mongoose.Schema

    const assetSchema = new Schema({
        attributes: [{
           // The attributes._field should reference one of the Partition field values
            _field: {
                type: Schema.Types.ObjectId,
                ref: 'Partition.fields' // &lt;-- THIS LINE
            },
            value: {
                type: Schema.Types.Mixed,
                required: true
            }
        }],
        // Reference the partition ID this asset belongs to
        _partition: {
            type: Schema.Types.ObjectId,
            ref: 'Partition'
        }
    })

    return Mongoose.model( 'Asset', assetSchema )
}
</code></pre>

<p>Where I'm running into issues, is with the <code>_field</code> item in the <code>Asset</code> schema. Im not sure what I should set as the <code>ref</code> value, since its referencing a sub-schema (meaning the <code>Field</code> schema within the <code>Partition</code> schema)</p>

<p>I may have overlooked it in the docs, but I didn't see anything. How can I reference a models sub-schema, so when I <a href="http://mongoosejs.com/docs/populate.html" rel="nofollow">populate</a> that item in the query, it populates it with the sub-documents inside the <code>Partition</code> model documents?</p>

<p>Ive tried to reference the field documents as <code>Partition.fields</code>, which resulted in the error:</p>

<blockquote>
  <p>MissingSchemaError: Schema hasn't been registered for model "Partition.fields".</p>
</blockquote>

<p>I tried the above <code>ref</code> value based on what I read from another SO thread, but it doesn't seem to work.</p>