首页 > 解决方案 > 如何从分层文档结构中展开/合并数组?

问题描述

我有一个嵌套的文档结构,我可以过滤它pluck以显示相关部分:

在此处输入图像描述

有没有一种优雅的方法可以将最后一级的所有条目合并到一个数组中?

预期结果(条目不是故意唯一的):

[
'3425b91f-f019-4db3-ad56-c336bf55279b', 
'3d07946e-183d-4992-9acd-676f5122e1b1', 
'3425b91f-f019-4db3-ad56-c336bf55279b', 
'3d07946e-183d-4992-9acd-676f5122e1b1', 
'2cd652a6-4dcd-4920-9592-d4cdc5a034bf', 
'70fe1812-e1de-447b-ac4f-d89fead4756d', 
'2cd652a6-4dcd-4920-9592-d4cdc5a034bf', 
'70fe1812-e1de-447b-ac4f-d89fead4756d'
]

我试着用

r.table('periods')['regions']['sites']['plants']['product']['process']['technologies'].run()

但它给出了错误“无法对序列序列执行括号”。

=> 是否有一些替代运算符可以为每个步骤获取合并序列而不是“序列序列”?

就像是

r.table('periods').unwind('regions.sites.plants.product.process.technologies')

这是一些用于创建示例数据的 python 代码:

from rethinkdb import RethinkDB
r = RethinkDB()
r.connect({}).repl()
r.table_create("periods")

def uniqueid():
    return r.uuid().run()


periodid_first = uniqueid()
periodid_second = uniqueid()


companyid_2000 = uniqueid()
companyid_2001 = uniqueid()

technologyid_2000_first = uniqueid()
technologyid_2000_second = uniqueid()

technologyid_2001_first = uniqueid()
technologyid_2001_second = uniqueid()

energy_carrierid_2000_first = uniqueid()
energy_carrierid_2000_second = uniqueid()

energy_carrierid_2001_first = uniqueid()
energy_carrierid_2001_second = uniqueid()

periods = [
    {
        'id': periodid_first,
        'start': 2000,
        'end': 2000,
        # 'sub_periods': [],
        'regions': [
            {
                'id': 'DE',
                # 'sub_regions': [],
                'sites': [
                    {
                        'id': 'first_site_in_germany',
                        'company': companyid_2000,  # => verweist auf periods => companies
                        'plants': [
                            {
                                'id': 'qux',
                                'product': {
                                    'id': 'Ammoniak',
                                    'process': {
                                        'id': 'SMR+HB',
                                        'technologies': [
                                           technologyid_2000_first,  # => verweist auf periods => technologies
                                           technologyid_2000_second
                                        ]
                                    }
                                }
                            }
                        ]
                    }
                ]
            },
            {
                'id': 'FR',
                # 'sub_regions': [],
                'sites': [
                    {
                        'id': 'first_site_in_france',
                        'company': companyid_2000,  # => verweist auf periods => companies
                        'plants': [
                            {
                                'id': 'qux',
                                'product': {
                                    'id': 'Ammoniak',
                                    'process': {
                                        'id': 'SMR+HB',
                                        'technologies': [
                                           technologyid_2000_first,  # => verweist auf periods => technologies
                                           technologyid_2000_second
                                        ]
                                    }
                                }
                            }
                        ]
                    }
                ]
            }
        ],
        'companies': [
            {
                'id': companyid_2000,
                'name': 'international_company'
            }
        ],
        'technologies': [
             {
                'id': technologyid_2000_first,
                'name': 'SMR',
                'specific_cost_per_year': 123,
                'specific_energy_consumptions': [
                    {
                        'energy_carrier': energy_carrierid_2000_first,
                        'specific_consumption': 5555
                    },  # => verweist auf periods => energy_carriers
                    {
                         'energy_carrier': energy_carrierid_2000_second,
                         'energy_consumption': 2333
                    }
                ]
             },
             {
                'id': technologyid_2000_second,
                'name': 'HB',
                'specific_cost_per_year': 1234,
                'specific_energy_consumptions': [
                    {
                        'energy_carrier': energy_carrierid_2000_first,
                        'specific_consumption': 555
                    },  # => verweist auf periods => energy_carriers
                    {
                         'energy_carrier': energy_carrierid_2000_second,
                         'energy_consumption': 233
                    }
                ]
             }
        ],
        'energy_carriers': [
            {
                'id': energy_carrierid_2000_first,
                'name': 'oil',
                'group': 'fuel'
            },
            {
                'id': energy_carrierid_2000_second,
                'name': 'gas',
                'group': 'fuel'
            },
            {
                'id': uniqueid(),
                'name': 'conventional',
                'group': 'electricity'
            },
            {
                'id': uniqueid(),
                'name': 'green',
                'group': 'electricity'
            }
        ],
        'networks': [
            {
                'id': uniqueid(),
                'name': 'gas',
                'sub_networks': [],
                'pipelines': [

                ]
            },
            {
                'id': uniqueid(),
                'name': 'gas',
                'sub_networks': [],
                'pipelines': [

                ]
            }
        ]
    },
    {
        'id': periodid_second,
        'start': 2001,
        'end': 2001,
        # 'sub_periods': [],
        'regions': [
            {
                'id': 'DE',
                # 'sub_regions': [],
                'sites': [
                    {
                        'id': 'first_site_in_germany',
                        'company': companyid_2001,  # => verweist auf periods => companies
                        'plants': [
                            {
                                'id': 'qux',
                                'product': {
                                    'id': 'Ammoniak',
                                    'process': {
                                        'id': 'SMR+HB',
                                        'technologies': [
                                           technologyid_2001_first,  # => verweist auf periods => technologies
                                           technologyid_2001_second
                                        ]
                                    }
                                }
                            }
                        ]
                    }
                ]
            },
            {
                'id': 'FR',
                # 'sub_regions': [],
                'sites': [
                    {
                        'id': 'first_site_in_france',
                        'company': companyid_2001,  # => verweist auf periods => companies
                        'plants': [
                            {
                                'id': 'qux',
                                'product': {
                                    'id': 'Ammoniak',
                                    'process': {
                                        'id': 'SMR+HB',
                                        'technologies': [
                                           technologyid_2001_first,  # => verweist auf periods => technologies
                                           technologyid_2001_second
                                        ]
                                    }
                                }
                            }
                        ]
                    }
                ]
            }
        ],
        'companies': [
            {
                'id': companyid_2001,
                'name': 'international_company'
            }
        ],
        'technologies': [
             {
                'id': technologyid_2001_first,
                'name': 'SMR',
                'specific_cost_per_year': 123,
                'specific_energy_consumptions': [
                    {
                        'energy_carrier': energy_carrierid_2001_first,
                        'specific_consumption': 5555
                    },  # => verweist auf periods => energy_carriers
                    {
                         'energy_carrier': energy_carrierid_2001_second,
                         'energy_consumption': 2333
                    }
                ]
             },
             {
                'id': technologyid_2001_second,
                'name': 'HB',
                'specific_cost_per_year': 1234,
                'specific_energy_consumptions': [
                    {
                        'energy_carrier': energy_carrierid_2001_first,
                        'specific_consumption': 555
                    },  # => verweist auf periods => energy_carriers
                    {
                         'energy_carrier': energy_carrierid_2001_second,
                         'energy_consumption': 233
                    }
                ]
             }
        ],
        'energy_carrieriers': [
            {
                'id': energy_carrierid_2001_first,
                'name': 'oil',
                'group': 'fuel'
            },
            {
                'id': energy_carrierid_2001_second,
                'name': 'gas',
                'group': 'fuel'
            },
            {
                'id': uniqueid(),
                'name': 'conventional',
                'group': 'electricity'
            },
            {
                'id': uniqueid(),
                'name': 'green',
                'group': 'electricity'
            }
        ],
        'networks': [
            {
                'id': uniqueid(),
                'name': 'gas',
                'sub_networks': [],
                'pipelines': [

                ]
            },
            {
                'id': uniqueid(),
                'name': 'gas',
                'sub_networks': [],
                'pipelines': [

                ]
            }
        ]
    }
]

r.table('periods') \
.insert(periods) \
.run()

有关的:

RethinkDB:​​RqlRuntimeError:无法对序列序列执行括号

标签: mergeaggregaterethinkdbunwind

解决方案


嵌套的concat_map结合r.row运算符和括号向下钻取可以解决问题:

r.table('periods') \
.concat_map(r.row['regions']) \
.concat_map(r.row['sites']) \
.concat_map(r.row['plants'])['product']['process'] \
.concat_map(r.row['technologies']) \
.run()

推荐阅读