mongodb - 从 js 数组中找到对应的对象,并在 mongodb 聚合中使用它的字段
问题描述
我有这个 JavaScript 数组:
Arr = [
{
a: {
match: {
urban: 1
},
threshold: 20,
amount: 300,
factor: 'impact_factor'
},
b: {
match: {
urban: 0
},
threshold: 30,
amount: 100,
factor: 'impact_factor_1'
}
},
{
c: {
match: {
family_type: 'solo_mayores'
},
threshold: 20,
amount: 300,
factor: 'impact_factor'
},
d: {
match: {
family_type: 'adultos_y_mayores'
},
threshold: 33,
amount: 100,
factor: 'impact_factor_1'
},
e1: {
match: {
family_type: 'bi-parental',
urban: 1
},
threshold: 22,
amount: 330,
factor: 'impact_factor'
},
e2: {
match: {
family_type: 'bi-parental',
urban: 0
},
threshold: 12,
amount: 333,
factor: 'impact_factor'
},
f: {
match: {
family_type: 'monoparental'
},
threshold: 14,
amount: 310,
factor: 'impact_factor'
}
}
]
数据在 mongodb 中看起来像这样(单行):
_id:5eecb9e406e5910084b07e54
uid:3529
weight:1
family_size:2
urban:1
family_type:"solo_mayores"
income_per_capita:35
impact_factor:0.005
factor_10-1:0.1
factor_1:1
factor_2:2
factor_3:3
factor_10:10
factor_urban_10_rural_1:10
factor_family_size:2
我想使用这个逻辑形成一个 $project 聚合或类似的聚合来获取一个新变量:
newVariable = income_per_capita + X * amount1 + Y * amouont2,
在哪里:
- X 是来自 Arr[0] 的因子(我们应该使用匹配字段匹配来自 Arr[0] 的对象并使用找到的对象的因子)
- Y 也是来自 Arr[1] 的因子(我们应该使用匹配字段匹配来自 Arr[1] 的对象并使用对象的因子)
- amount1 是来自 Arr[0] 的对象的数量
- amount2 是来自 Arr[1] 的对象的数量
如何首先使用匹配字段从数组中找到一个对象,然后使用它在 $project 中的字段来获取新值?
更新
现在我有这个对象:
var policies = {
urban: [
{
id: 'a',
name: 'urban',
value: 0,
threshold: 20,
amount: 300,
factor: 'impact_factor'
},
{
id: 'b',
name: 'urban',
value: 1,
threshold: 30,
amount: 100,
factor: 'factor_1'
}
],
family_type: [
{
id: 'c',
name: 'family_type',
value: 'solo_mayores',
threshold: 20,
amount: 300,
factor: 'impact_factor'
},
{
id: 'd',
name: 'family_type',
value: 'adultos_y_mayores',
threshold: 33,
amount: 100,
factor: 'factor_1'
},
{
id: 'e1',
name: 'family_type',
value: 'bi-parental',
threshold: 22,
amount: 330,
factor: 'impact_factor'
},
{
id: 'f',
name: 'family_type',
value: 'monoparental',
threshold: 14,
amount: 310,
factor: 'impact_factor'
}
]
};
然后我使用这个聚合使它工作:
[
{
"$project": {
"_id": 0,
"factor_1": 1,
"impact_factor": 1,
"urban": 1,
"family_type": 1,
"urban_": {
"$let": {
"vars": {
"input": [
{
"id": "a",
"name": "urban",
"value": 0,
"threshold": 20,
"amount": 300,
"factor": "impact_factor"
},
{
"id": "b",
"name": "urban",
"value": 1,
"threshold": 30,
"amount": 100,
"factor": "factor_1"
}
]
},
"in": {
"$setDifference": [
{
"$map": {
"input": "$$input",
"as": "output",
"in": {
"$cond": [
{
"$eq": [
"$$output.value",
"$urban"
]
},
{
"amount": "$$output.amount",
"factor": "$$output.factor"
},
false
]
}
}
},
[
false
]
]
}
}
},
"family_type_": {
"$let": {
"vars": {
"input": [
{
"id": "c",
"name": "family_type",
"value": "solo_mayores",
"threshold": 20,
"amount": 300,
"factor": "impact_factor"
},
{
"id": "d",
"name": "family_type",
"value": "adultos_y_mayores",
"threshold": 33,
"amount": 100,
"factor": "factor_1"
},
{
"id": "e1",
"name": "family_type",
"value": "bi-parental",
"threshold": 22,
"amount": 330,
"factor": "impact_factor"
},
{
"id": "f",
"name": "family_type",
"value": "monoparental",
"threshold": 14,
"amount": 310,
"factor": "impact_factor"
}
]
},
"in": {
"$setDifference": [
{
"$map": {
"input": "$$input",
"as": "output",
"in": {
"$cond": [
{
"$eq": [
"$$output.value",
"$family_type"
]
},
{
"amount": "$$output.amount",
"factor": "$$output.factor"
},
false
]
}
}
},
[
false
]
]
}
}
}
}
},
{
"$unwind": "$urban"
},
{
"$unwind": "$family_type"
}
]
这会返回:
[
{
"urban": 1,
"family_type": "solo_mayores",
"impact_factor": 0.005,
"factor_1": 1,
"urban_": [
{
"amount": 100,
"factor": "factor_1"
}
],
"family_type_": [
{
"amount": 300,
"factor": "impact_factor"
}
]
},
....
]
现在我想将 $$output.amount 乘以 $document[$$output.factor]。
如何根据 $$output.factor 动态访问 mongodb 文档的字段?
解决方案
推荐阅读
- java - 为什么我不能将数据添加到所有列?
- flutter - 如何将图像放入警报对话框?
- java - Java如何并行迭代HashMap的所有值
- javascript - Select2.js 属性变化
- couchbase - 如果使用 N1QL 查询未找到结果,如何在 couchbase 中获取归档名称(列名)?
- java - 将嵌套的分隔字符串转换为对象
- postgresql - 无法使用 R2DBC Postgresql WebFlux 获得响应
- angular - Angular Guard 失效
- amazon-web-services - 如何解决 API 网关的 CORS 问题?
- python-3.x - 在 Python 中使用 BS4 或 Newspaper3k 抓取元标记