首页 > 解决方案 > 从 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,

在哪里:

如何首先使用匹配字段从数组中找到一个对象,然后使用它在 $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 文档的字段?

标签: mongodbaggregation-framework

解决方案


推荐阅读