Merge pull request #21856 from simonxca/fix-mongoose

Set mongoose document id property to any
This commit is contained in:
Arthur Ozga
2017-12-19 16:10:13 -08:00
committed by GitHub
2 changed files with 27 additions and 2 deletions

View File

@@ -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 {

View File

@@ -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<ImageDoc>('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();
}
});