对于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' });发布于 2018-09-26 14:09:32
对于Mongoose 5.2+,您可以将该值作为API Doc中指定的第三个参数进行传递
const ClickEvent = Event.discriminator('ClickEvent', new Schema({
url: { type: String }
}), "click")并将kind设置为"click“
https://stackoverflow.com/questions/48411555
复制相似问题