diff --git a/lib/options/SchemaTypeOptions.js b/lib/options/SchemaTypeOptions.js index b19f7b2e7ca..20a33213df2 100644 --- a/lib/options/SchemaTypeOptions.js +++ b/lib/options/SchemaTypeOptions.js @@ -119,6 +119,19 @@ Object.defineProperty(SchemaTypeOptions.prototype, 'default', opts); Object.defineProperty(SchemaTypeOptions.prototype, 'ref', opts); +/** + * The path in the document that `populate()` should use to find the model + * to use. + * + * @api public + * @property ref + * @memberOf SchemaTypeOptions + * @type Function|String + * @instance + */ + + Object.defineProperty(SchemaTypeOptions.prototype, 'refPath', opts); + /** * Whether to include or exclude this path by default when loading documents * using `find()`, `findOne()`, etc. diff --git a/test/types/populate.test.ts b/test/types/populate.test.ts index 122d5ec3168..6963e915342 100644 --- a/test/types/populate.test.ts +++ b/test/types/populate.test.ts @@ -1,4 +1,4 @@ -import { Schema, model, Document, PopulatedDoc, Types, HydratedDocument } from 'mongoose'; +import mongoose, { Schema, model, Document, PopulatedDoc, Types, HydratedDocument, SchemaTypeOptions } from 'mongoose'; // Use the mongodb ObjectId to make instanceof calls possible import { ObjectId } from 'mongodb'; import { expectAssignable, expectError, expectType } from 'tsd'; @@ -205,6 +205,9 @@ function gh11862() { userType: string; friend: Types.ObjectId; } + + const t: SchemaTypeOptions = { type: 'ObjectId', refPath: 'userType' }; + const userSchema = new Schema({ userType: String, friend: { type: 'ObjectId', refPath: 'userType' }