2014-09-24 21 views
18

Ben firavunfaresi ile başlayan ve bu tür yapılandırmayı nasıl bilmek istiyorum:Yabancı anahtar firavunfaresi

:

enter image description here

bir tarif farklı bileşen

Benim iki model var var

İçerik ve Reçete:

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); 
+0

Sana nasıl soruyorsunuz sanırım Ref operatörünü kullanmak için? – Tim

cevap

36

Kontrol Özellikle bu bölümünde aşağıda Güncelleme kod: {türü: Schema.Types.ObjectId, ref: 'Madde'}

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); 

Kaydetmek İçin:

var r = new Recipe(); 

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

r.save(); 
+0

Nasıl kaydederim? Bu an için, böyle tasarruf am: .post (fonksiyonu (req, S) { var tarifi = yeni tarifi(); recipe.name = req.body.name; recipe.save (fonksiyonu (err) { if (err) res.send (err); res.json ({mesajı:)} 'tarifi oluşturulan'; }); }) – devtest654987

+0

ayrı ayrı maddeler kaydedebilir ve daha sonra içine id itmek gerekir dizi. – Tim

+0

Thx benim öz tarafından denemek var ve yardım etmek – devtest654987