2013-12-20 7 views
1

notifications을 여러 개 보유 할 수있는 users 모델이 있습니다. NotificationSchema에서 notifier은 사용자 ID를 보유하고 있으며 users 모델을 참조합니다. 다음 쿼리를 실행할 때 :몽구스 - 참조 된 모델의 필드를 제한

User.find().populate('notifications.notifier').exec(function(err,users){ 
    //users[0].notifications[0].notifier  
    //I am getting all fields from the referenced user 
    //I don't want the whole document but some fields only 
}); 

어떻게 누군가가 특정 모델을 참조 할 때 사용할 수있는 필드를 제한/제한 할 수 있습니까? 여기

내가 NotificationSchema에 대해 별도의 model이없는, BTW users 모델

var NotificationSchema =new Schema({ 
    notifier : {type:Schema.Types.ObjectId, ref:'users'}, 
    //How could I say : 
    //notifier : {type:Schema.Types.ObjectId, ref:'users', fields:['_id', 'name']} 
    //because I know what fields do I need from referenced model for this Schema. 

    __SOME__ 
    __OTHER__ 
    __FIELDS__ 
}); 

var UsersSchema = new Schema({ 
    name : {given:String, middle:String, family:String} 
    email:String, 
    password:String, 
    notifications : [NotificationSchema] 
}); 

var Users = mongoose.model('users', UsersSchema); 

입니다.

이 기능을 사용할 수없는 경우 어떻게 수동으로 구현할 수 있습니까? 나는 몇몇 문서를 놓쳤는가? 제게는 robust 방법을 알려주세요.

답변

0

은 내가 필드 선택에 the documentation

User.find().populate('notifications.notifier', '_id name').exec(function(err, users) { 
//users[0].notifications[0].notifier   ^^^^^^^^^ HOW FUNNY 
//Yes I am getting '_id' and 'name' fileds only 
}); 
섹션 답을 발견

몽구스 문서에 그것을 발견