Reputation: 885
I have two Schemas Organization
and Location
. On Organization
Schema i have referenced the Location Schema:
Organization = new Schema({
location: {
type: Schema.ObjectId,
ref: 'Location'
}
});
Location
schema has fields like country,state, city, district etc.
Now how would I search the Organization
with specific country or state or city.
What I have found in the Mongoose Document is this way:
Organization
.find()
.populate([{path:'location',select:'country state city district' },
{/*other population paths*/}] /*Some constriants here*/).exec(function(err, orgs){
console.log(orgs);
});
How do I achieve this type of search. Are there any plugins available for this type of reference document search?
Upvotes: 0
Views: 169
Reputation: 4054
You can pass match query to select the particular location. Please read the docs 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);
});
Upvotes: 2