mongoose自动在ref字段中创建子文档

时间:2018-04-20 12:22:47

标签: node.js mongoose npm ecmascript-6

考虑这个架构:

文件1:

const dishSchema = new Schema({
    name: {type: String, required: true}
}
class Dish extends Model {}


dishSchema.loadClass(Dish);
module.exports = mongoose.model('Dish', dishSchema);

file2的:

Const offerSchema = new Schema({
    dish: {
        type: Schema.Types.ObjectId,
        ref: 'Dish'
    }
class Offer extends Model {}


offerSchema.loadClass(Offer);
module.exports = mongoose.model('Offer', offerSchema);

现在我需要创建一道菜,然后创建一个带有该菜肴ID的优惠:Dish.create({name: "foo}).then(dish => Offer.create({dish: dish.id}))

有没有人知道自动化这种模式的任何包或选项? 所以最终的过程将是这样的:Offer.create({dish: {name: "foo"}})

使用dish作为embeded文档是不可能的,因为我使用它任何许多其他模式

由于

0 个答案:

没有答案