title: Mongoose can not reference refPath in root path
body: <h3>schema</h3>
  <pre><code>    const commentSchema = new Schema({
        body: { type: String, required: true },
        on: {
          type: Schema.Types.ObjectId,
          required: true,
          // Instead of a hardcoded model name in `ref`, `refPath` means Mongoose
          // will look at the `onModel` property to find the right model.
          refPath: 'onModel'
        },
        onModel: {
          type: String,
          required: true,
          enum: ['BlogPost', 'Product']
        }
      });
      
      const Product = mongoose.model('Product', new Schema({ name: String }));
      const BlogPost = mongoose.model('BlogPost', new Schema({ title: String }));
      const Comment = mongoose.model('Comment', commentSchema);
  </code></pre>
  <h3>populating</h3>
  <pre><code>    const book = await Product.create({ name: 'The Count of Monte Cristo' });
      const post = await BlogPost.create({ title: 'Top 10 French Novels' });
      
      const commentOnBook = await Comment.create({
        body: 'Great read',
        on: book._id,
        onModel: 'Product'
      });
      
      const commentOnPost = await Comment.create({
        body: 'Very informative',
        on: post._id,
        onModel: 'BlogPost'
      });
      
      // The below `populate()` works even though one comment references the
      // 'Product' collection and the other references the 'BlogPost' collection.
      const comments = await Comment.find().populate('on').sort({ body: 1 });
      comments[0].on.name; // &quot;The Count of Monte Cristo&quot;
      comments[1].on.title; // &quot;Top 10 French Novels&quot;
  </code></pre>
  <p>The link below is where that i get this test code</p>
  <p><a href="https://mongoosejs.com/docs/5.x/docs/populate.html#dynamic-ref" rel="nofollow noreferrer">https://mongoosejs.com/docs/5.x/docs/populate.html#dynamic-ref</a></p>
  <p>when i ran this code there has some error</p>
  <blockquote>
  <p>MissingSchemaError: Schema hasn't been registered for model ...</p>
  </blockquote>
  <p>When i change code like this?</p>
  <h3>changed schema</h3>
  <pre><code>    const commentSchema = new Schema({
            body: { type: String, required: true },
            cover : {
                     on: {
                            type: Schema.Types.ObjectId,
                            required: true,
                            // Instead of a hardcoded model name in `ref`, `refPath` means Mongoose
                            // will look at the `onModel` property to find the right model.
                            refPath: 'cover.onModel'
                          },
                     onModel: {
                            type: String,
                            required: true,
                            enum: ['BlogPost', 'Product']
                           }
                   }
          });
          
          const Product = mongoose.model('Product', new Schema({ name: String }));
          const BlogPost = mongoose.model('BlogPost', new Schema({ title: String }));
          const Comment = mongoose.model('Comment', commentSchema);
  </code></pre>
  <h3>changed populating</h3>
  <pre><code>    const book = await Product.create({ name: 'The Count of Monte Cristo' });
      const post = await BlogPost.create({ title: 'Top 10 French Novels' });
      
      const commentOnBook = await Comment.create({
        body: 'Great read',
        cover : {
              on: book._id,
              onModel: 'Product'
        }
      });
      
      const commentOnPost = await Comment.create({
        body: 'Very informative',
        cover : {
              on: post._id,
              onModel: 'BlogPost'
        }
      });
      
      // The below `populate()` works even though one comment references the
      // 'Product' collection and the other references the 'BlogPost' collection.
      const comments = await Comment.find().populate('cover.on').sort({ body: 1 });
      comments[0].on.name; // &quot;The Count of Monte Cristo&quot;
      comments[1].on.title; // &quot;Top 10 French Novels&quot;
  </code></pre>
  <p>My solution was covering each on , onModel with other object. But I want to know is there any other solutions or why it happens like this. Thanks!</p>