Mongoose two level population using KeystoneJs [duplicate]
Asked Answered
R

1

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?

Rile answered 24/8, 2015 at 5:9 Comment(1)
it may not be duplicate, because keystone.js queries are differentWilsey
J
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)
                    });

        });
Johnette answered 24/8, 2015 at 5:27 Comment(3)
Wonderful! Thanks for your reply. I was pretty close and almost there. Appreciate your help.Rile
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 codeWilsey
.populate({ path: 'countries', populate: { path: 'cities', } })Grandiloquence

© 2022 - 2024 — McMap. All rights reserved.