sql – 外键猫鼬

前端之家收集整理的这篇文章主要介绍了sql – 外键猫鼬前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我从麻雀开始,我想知道如何做这种类型的配置:

食谱有不同的成分

我有两个模型:

成分和食谱:

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();
原文链接:https://www.f2er.com/mssql/81815.html

猜你在找的MsSQL相关文章