diff --git a/types/mongoose/index.d.ts b/types/mongoose/index.d.ts index 7eb768f3c8..2e8b7c541c 100644 --- a/types/mongoose/index.d.ts +++ b/types/mongoose/index.d.ts @@ -1154,8 +1154,13 @@ declare module "mongoose" { } interface MongooseDocumentOptionals { - /** The string version of this documents _id. */ - id?: string; + /** + * Virtual getter that by default returns the document's _id field cast to a string, + * or in the case of ObjectIds, its hexString. This id getter may be disabled by + * passing the option { id: false } at schema construction time. If disabled, id + * behaves like any other field on a document and can be assigned any value. + */ + id?: any; } interface DocumentToObjectOptions { diff --git a/types/mongoose/mongoose-tests.ts b/types/mongoose/mongoose-tests.ts index 9fe20cd8ad..57c98fcbbf 100644 --- a/types/mongoose/mongoose-tests.ts +++ b/types/mongoose/mongoose-tests.ts @@ -503,6 +503,25 @@ doc.populate(cb); doc.populate({path: 'hello'}).execPopulate().catch(cb); doc.update({$inc: {wheels:1}}, { w: 1 }, cb); +const ImageSchema = new mongoose.Schema({ + name: {type: String, required: true}, + id: {type: Number, unique: true, required: true, index: true}, +}, { id: false }); + +interface ImageDoc extends mongoose.Document { + name: string, + id: number +} + +const ImageModel = mongoose.model('image', ImageSchema); + +ImageModel.findOne({}, function(err, doc) { + if (doc) { + doc.name; + doc.id; + } +}); + /* * section types/subdocument.js * http://mongoosejs.com/docs/api.html#types-subdocument-js @@ -1277,6 +1296,7 @@ mongoModel.remove(function (err, product) { if (err) throw(err); MongoModel.findById(product._id, function (err, product) { if (product) { + product.id.toLowerCase(); product.remove(); } });