3

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?

ChrisRich
  • 8,300
  • 11
  • 48
  • 67

1 Answers1

8

In mongoose you can do this way:

regionModel.find().populate("countries").exec(function(err, regions){

    if(err){
        throw err;
    }

    // Regions with populate countries
    cityModel.populate(regions, {
        path: 'countries.cities',
        select: '_id name'
    },function(err, regions) {

        //Regions with Countries and Populated Cities

    });

})

Actually i dont familiar with keystone syntax, but i try to convert it to keystone syntax. Hope it works, if not please try to convert above code equivalent to keystonejs

keystone.list('Region').model.find()
        .populate('countries')
        .exec(function(err, regions){

            if(err){
                throw err;
            }

            keystone.list('City').model.find()
                    .populate('cities')
                    .exec(function(err, regions){
                        console.log(regions)
                    });

        });
Hiren S.
  • 2,793
  • 15
  • 25
  • Wonderful! Thanks for your reply. I was pretty close and almost there. Appreciate your help. – ChrisRich Aug 24 '15 at 07:47
  • how at all this is possible, its clear that the aboe code is syntextically wrong for kewystone. so If you think it worked then can you put the actualy keystone related code – enRaiser Aug 28 '16 at 06:12
  • .populate({ path: 'countries', populate: { path: 'cities', } }) – Sefi Grossman Jul 20 '17 at 11:27