【发布时间】:2020-07-17 17:52:20
【问题描述】:
我在 MongoDB 中有下一个示例文档。
db={
"contracts": [
{
"bid": 1, // id in businesses collection
"type": "A",
"name": "N1"
},
{
"bid": 1,
"type": "B",
"name": "N2"
},
{
"bid": 1,
"type": "C",
"name": "N3"
}
],
"businesses": [
{
"id": 1,
"contract_settings": {
"A": {
"price": 100
},
"B": {
"price": 200
},
"default": "A"
}
}
]
}
我想根据合同的类型查找合同的价格。如果合约的类型不在contract_settings中,那么我应该使用默认值。
例如对于当前方案,我希望输出为
"contracts": [
{
"bid": 1,
"type": "A",
"name": "N1",
"price": 100
},
{
"bid": 1,
"type": "B",
"name": "N2",
"price": 200
},
{
"bid": 1,
"type": "C",
"name": "N3",
"price":100 // because default settings are settings for type "A"
}
]
}
Contract_settings 总是有一些类型并且“默认”总是连接到现有类型。
是否可以使用字段值(方案中的contracts.type)作为字段名从businesss.contract_settings 中获取设置?
注意,contract_settings 可以包含任意名称,所以我不能使用这样的解决方案 similar problem
PS。如果 contract_settings 是 jsonb 字段,并且使用这样的代码,可以解决 postgres 中的相同问题
((CASE WHEN businesses.contract_settings::jsonb ? contracts.contract_type::text
THEN businesses.contract_settings -> contracts.contract_amount::text
ELSE businesses.contract_settings -> (businesses.contract_settings ->> 'default') END)->>'price')::double precision
【问题讨论】:
标签: mongodb aggregation-framework