使用KeystoneJs的猫鼬两级人口 [英] Mongoose two level population using KeystoneJs

查看:78
本文介绍了使用KeystoneJs的猫鼬两级人口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要用Mongoose/Keystone填充两个级别,但是遇到了障碍.

I need to populate two levels down with Mongoose / Keystone, but have hit a road block.

我有3个模型:地区,国家和城市. 地区包含国家,国家包含城市.

I have 3 models: Region, Country and City. Regions contains countries and countries contain cities.

我的模特:

模型区域:

var Region = new keystone.List('Region');

Region.add({
      name: {type: Types.Text}
    , countries: {type: Types.Relationship, ref: 'Country', many: true}
});

模范国家

var Country = new keystone.List('Country');

Country.add({
      name: {type: Types.Text}
    , cities: {type: Types.Relationship, ref: 'City', many: true}
});

模范城市

var City = new keystone.List('City');

City.add({
      name: {type: Types.Text}
});

查询:

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

收益:

    {
        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?

推荐答案

在猫鼬中,您可以这样做:

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

    });

})

实际上我不熟悉梯形失真语法,但是我尝试将其转换为梯形失真语法.希望它能奏效,否则请尝试转换与keystonejs等效的上述代码

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

        });

这篇关于使用KeystoneJs的猫鼬两级人口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆