2014-12-26 2 views
-1

두 개의 스키마 OrganizationLocation이 있습니다. Organization 스키마에 나는 위치 스키마를 참조한 :mongoosejs에서 참조 된 문서를 검색하는 방법

Organization = new Schema({ 
    location: { 
    type: Schema.ObjectId, 
    ref: 'Location' 
    } 
}); 

Location 스키마 나 특정 국가 또는 주 또는 도시로 Organization을 어떻게 찾을 지 등 국가, 주, 도시, 지역 이제

같은 필드가 있습니다. 나는이 형태의 검색을 달성하려면 어떻게

Organization 
.find() 
.populate([{path:'location',select:'country state city district' }, 
{/*other population paths*/}] /*Some constriants here*/).exec(function(err, orgs){ 
    console.log(orgs); 
}); 

: 나는 몽구스 문서에서 발견 한 어떤

이 방법입니다. 이 유형의 참조 문서 검색에 사용할 수있는 플러그인이 있습니까?

답변

1

일치 검색어를 전달하여 특정 위치를 선택할 수 있습니다. 문서 query_populate을 읽어보십시오.

Organization 
.find() 
.populate([{ 
    path:'location', 
    match : {state : req.query.state} //your query goes here 
    select:'country state city district' 
}]).exec(function(err, orgs){ 
    console.log(orgs); 
}); 
관련 문제