2016-08-03 7 views
0

당신은 다음 데이터 구조를 가지고?몽구스, 컬렉션의 개체를 참조 경우

당신이 원하는 행동을 얻기 위하여
const Item = mongoose.model('Item', mongoose.Schema({ 
    name: String, 
    branch: {type: mongoose.Schema.ObjectId, ref: 'Company.branches'} 
})); 
+0

회사 모델의 모든 지점 또는 일부 기준에 따라 특정 지점에 액세스 하시겠습니까? –

+0

'Item' 엔티티에 저장된 브랜치의'_id'에 기반하여 하나의 브랜치 만 필요합니다. –

답변

0

, 별도의 지점 모델을 만들 수 있습니다 다음 항목 모델은 다음과 같을 것이다 :

const Item = mongoose.model('Item', mongoose.Schema({ 
    name: String, 
    branch: {type: mongoose.Schema.ObjectId, ref: 'Branch'} 
})); 

귀하의 회사 스키마는 다음과 같을 것이다 :

const branchSchema = mongoose.Schema({ name: String }); 
const Company = mongoose.model('Company', mongoose.Schema({ 
    name: String, 
    branches: [{type: mongoose.Schema.ObjectId, ref: 'Branch'}] 
})); 

그런 다음 찾기를 수행 할 때 지점과 관련된 정보를 얻으려면 .populate()을 사용하십시오.

관련 문제