有了Mongoose,我知道我可以使用鉴别器功能,使用鉴别器键在同一个集合中拥有不同的模式。
const options = { discriminatorKey: 'kind' };
const Event = mongoose.model('Event, new Schema({
name: { type: String }
}, options);
const ClickEvent = Event.discriminator('ClickEvent', new Schema({
url: { type: String }
}, options);
// on another file
const ClickEvent = mongoose.model('ClickEvent');
const clickEvent = new ClickEvent({
name: 'sir',
url: 'http://somewhere.com/hello'
});
console.log(clickEvent); // { name: 'sir', url: 'http://somewhere.com/hello', kind: 'ClickEvent' }
// look carefully kind is set to ClickEvent
我想要的是
kind
设置为
click
以某种方式
我该怎么做?我希望这样:
const ClickEvent = Event.discriminator('ClickEvent', new Schema({
url: { type: String }
}, { ...options, discriminatorValue: 'click' });