Mongoose two level population using KeystoneJs [duplicate]

时光总嘲笑我的痴心妄想 提交于 2019-12-22 22:39:08

问题


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?


回答1:


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)
                    });

        });


来源:https://stackoverflow.com/questions/32174803/mongoose-two-level-population-using-keystonejs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!