I need to populate two levels down with Mongoose / Keystone, but have hit a road block.
I have 3 models: Region, Country and City. Regions contains countries and countries contain cities.
My models:
Model Region:
var Region = new keystone.List('Region');
Region.add({
name: {type: Types.Text}
, countries: {type: Types.Relationship, ref: 'Country', many: true}
});
Model Country
var Country = new keystone.List('Country');
Country.add({
name: {type: Types.Text}
, cities: {type: Types.Relationship, ref: 'City', many: true}
});
Model City
var City = new keystone.List('City');
City.add({
name: {type: Types.Text}
});
Query:
keystone.list('Region').model.find()
.populate('countries')
.exec(function(err, regions){
console.log(regions)
});
Yields:
{
name: 'Oceania',
countries: [
{
_id: 55d9b260415baa6958ac04c1
name: 'Australia',
cities: [
_id: 55d9b260415baa6958ac04c2,
_id: 55d9b260415baa6958ac04c3,
_id: 55d9b260415baa6958ac04c4
]
},
{
_id: 55d9b260415baa6958ac04c5
name: 'New Zealand',
cities: [
_id: 55d9b260415baa6958ac04c6,
_id: 55d9b260415baa6958ac04c7
]
}
]
},
{
name: 'Americas',
countries: [
{
_id: 55d9b260415baa6958ac04c1
name: 'USA',
cities: [
_id: 55d9b260415baa6958ac04d2,
_id: 55d9b260415baa6958ac04d3,
_id: 55d9b260415baa6958ac04d4
]
},
{
_id: 55d9b260415baa6958ac04c5
name: 'Canada',
cities: [
_id: 55d9b260415baa6958ac04e6,
_id: 55d9b260415baa6958ac04e7
]
}
]
}
]
How would I populate the cities? As far as I understand Mongoose does not support deep population.
Can I query the results then or how?