加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

sql – 外键猫鼬

发布时间:2020-12-12 16:40:55 所属栏目:MsSql教程 来源:网络整理
导读:我从麻雀开始,我想知道如何做这种类型的配置: 食谱有不同的成分 我有两个模型: 成分和食谱: var mongoose = require('mongoose');var Schema = mongoose.Schema;var IngredientSchema = new Schema({ name: String});module.exports = mongoose.model('Ing
我从麻雀开始,我想知道如何做这种类型的配置:

食谱有不同的成分

我有两个模型:

成分和食谱:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var IngredientSchema = new Schema({
    name: String
});

module.exports = mongoose.model('Ingredient',IngredientSchema);


var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var RecipeSchema = new Schema({
    name: String
});

module.exports = mongoose.model('Recipe',RecipeSchema);

解决方法

检查下面的更新代码,特别是这部分:{type:Schema.Types.ObjectId,ref:’Ingredient’}
var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var IngredientSchema = new Schema({
    name: String
});

module.exports = mongoose.model('Ingredient',IngredientSchema);


var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var RecipeSchema = new Schema({
    name: String,ingredients:[
      {type: Schema.Types.ObjectId,ref: 'Ingredient'}
    ]
});

module.exports = mongoose.model('Recipe',RecipeSchema);

保存:

var r = new Recipe();

r.name = 'Blah';
r.ingredients.push('mongo id of ingredient');

r.save();

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读