[英]How to inherit a mongoose model and store it in a different collection
I need to create a model (NEW) which is almost identical to an existing one (ORIGINAL). 我需要创建一个与现有模型(原始)几乎完全相同的模型(新)。 The NEW model will have a new field ("expiresAt") that I want to use for MongoDB Time To Live (TTL) feature.
新模型将具有一个新字段(“ expiresAt”),我想将其用于MongoDB生存时间(TTL)功能。 For design purposes I don't want to store the NEW model in the same collection as ORIGINAL (only NEW model should have TTL feature).
出于设计目的,我不想将NEW模型存储在与ORIGINAL相同的集合中(仅NEW模型应具有TTL功能)。
Example: 例:
//ORIGINAL schema
var OriginalSchema = new Schema({
id: { type: Schema.Types.ObjectId },
name: { type: String },
description: { type:String },
});
//NEW schema
var NewSchema = new Schema({
id: { type: Schema.Types.ObjectId },
name: { type: String },
description: { type:String },
expiresAt: {
type: Date,
default: Date.now,
expires: 24*60*60
}
});
How can be this achieve by using inheritance? 如何通过使用继承来实现?
Mongoose discriminators is a schema inheritance mechanism. 猫鼬鉴别符是一种模式继承机制。 They enable you to have multiple models with overlapping schemas on top of the same underlying MongoDB collection.
它们使您可以在同一基础MongoDB集合的顶部拥有具有重叠模式的多个模型。 Some details were talked in this link .
在此链接中讨论了一些细节。
var OriginalSchema = new Schema({
id: { type: Schema.Types.ObjectId },
name: { type: String },
description: { type:String },
});
var NewSchema = new Schema({
expiresAt: {
type: Date,
default: Date.now,
expires: 24*60*60
}
});
var Original = mongoose.model('Original', OriginalSchema);
var NewSch = Original.discriminator('NewSch', NewSchema);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.