2013-08-28 11 views
0

중첩 참조 모델을 어떻게 채울 수 있습니까? 예를 들어몽구스 : 중첩 된 ref 모델 모집단

:

// 'Collection' model 
var CollectionSchema = new Schema({ 
    collection_name: String, 
    _groups: [{ type: Schema.Types.ObjectId, ref: 'Group' }], 
}); 

// 'Group' model 
var GroupSchema = new Schema({ 
    group_name: String, 
    _item: { type: Schema.Types.ObjectId, ref: 'Item' } // To be populated 
    _meta: [ { type: Schema.Types.ObjectId, ref: 'Meta' } // To be populated 
}); 

// 'Item' model 
var ItemSchema = new Schema({ 
    item_name: String, 
    item_description: String 
}); 

// 'Meta' model 
var MetaSchema = new Schema({ 
    meta_name: String, 
    meta_value: String 
}); 
나는 "컬렉션"모델의 각 "_item"내부 "_group"를 채우는 싶습니다

.

{ 
collection_name: "I'm a collection" 
_groups: [ 
      { _id: ObjectId("520eabd1da5ff8283c000009"), 
       group_name: 'Group1', 
       _item: { _id: ObjectId("520eabd1da5ff8283c000004"), 
         item_name: "Item1 name", 
         item_description: "Item1 description" 
        }, 
      _meta: { 
         _id: ObjectId("520eabd1da5ff8283c000001"), 
         meta_name= "Metadata name", 
         meta_value = "metadata value" 
        } 
      }, 
      { _id: ObjectId("520eabd1da5ff8283c000003"), 
       group_name: 'Group2', 
       _item: { _id: ObjectId("520eabd1da5ff8283c000002"), 
         item_name: "Item2 name", 
         item_description: "Item2 description" 
        }, 
      _meta: { 
         _id: ObjectId("520eabd1da5ff8283c000001"), 
         meta_name= "Metadata name", 
         meta_value = "metadata value" 
        } 
      } 
      ] 
} 

답변

2
var Group = mongoose.model('Group', GroupSchema); 
Group.find().populate('_item').populate('_meta').exec(function (error, groups) { 
    //groups will be an array of group instances and 
    // _item and _meta will be populated 
}); 
5

차라리

var Group = mongoose.model('Group', GroupSchema); 

Group.find().populate('_item _meta').exec(function (error, groups) { 
    // ... 
}); 
할 것 : 나는 이런 식으로 뭔가를 얻기 의미