Allow overriding id attribute

This commit is contained in:
simonxca
2017-11-29 22:13:11 -08:00
parent 7c0a078ce9
commit bf99493e11
2 changed files with 21 additions and 1 deletions

View File

@@ -1144,7 +1144,7 @@ declare module "mongoose" {
interface MongooseDocumentOptionals {
/** The string version of this documents _id. */
id?: string;
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();
}
});