2016-07-20 2 views

답변

0

mongoose 버전 4.5에서 소개 된 가상 채우기로이 작업을 수행 할 수 있습니다. 이를 위해서는 몽구스 스키마에서 가상 필드를 정의해야합니다.

var GallerySchema = new mongoose.Schema({ 
    name: String, 
    objectId: { 
     type: mongoose.Schema.Types.ObjectId 
    }, 
}); 

GallerySchema.virtual('user', { 
    ref: 'User', 
    localField: 'objectId', 
    foreignField: '_id' 
}); 

검색 쿼리를 실행할 때 사용자로 채우십시오. 코드 위

Gallry.find({_id: galleryId}).populate('user','firstName lastName').exec(function(error, gallery) { 
    console.log(error); 
    console.log(gallery);; 
}); 

당신은 링크

http://mongoosejs.com/docs/populate.html

아래에 채우는 가상 몽구스에 대한 자세한 내용을 얻을 수 있습니다, 오타가있을 수 있습니다 프로그램에서 테스트되지 않은 상태입니다