Mongo 聚合从字符串中获取索引值

问题描述 投票:0回答:1

这是示例文档

{   
  "pId":12345,    
     "charges": {
         "key1": "10.0,11.0,12.0,13.0,10.0,10.0,10.0,10.0,10.0,10.0,10.0,10.0,10.0,10.0,10.0,10.0,10.0,10.0,10.0,10.0,10.0,10.0,10.0,10.0,10.0,10.0,10.0,10.0,10.0,10.0",
         "key2": "22.5%,12.5%,22.5%,22.5%,22.5%,22.5%,22.5%,22.5%,22.5%,22.5%,22.5%,22.5%,22.5%,22.5%,22.5%,22.5%,22.5%,22.5%,22.5%,22.5%,22.5%,22.5%,22.5%,22.5%,22.5%,22.5%,22.5%,22.5%,22.5%,22.5%",
         "key3": "271.95,371.95,271.95,271.95,271.95,271.95,271.95,271.95,271.95,271.95,271.95,271.95,271.95,271.95,271.95,271.95,271.95,271.95,271.95,271.95,271.95,271.95,271.95,271.95,271.95,271.95,271.95,394.45,271.95,394.45",
         ...
    }
} 

充电键是动态的。我想从费用中获取请求的索引值

例如:输入索引为 (1, 2) 预期的结果是。

{   
  "pId":12345,    
     "charges": {
         "key1": "11.0,12.0",
         "key2": "12.5%,22.5%",
         "key3": "371.95,271.95",
         ...
    }
} 

我尝试过这个投影

第一阶段:

{ "$project" : { "pId" : 1, "费用" : { "key1" : { "$split" : ["$$charges.key1", ","]}}}},

第二阶段:

{ "$project" : { "pId" : 1, "费用" : "$concat" : [{ "$arrayElemAt" : ["$$charges.key1", 1]}, ",", { "$arrayElemAt" : ["$$charges.key1", 2]}]}}}]}

对于这个投影,我需要知道键名称并且需要提及投影中的所有键。是否有其他方法可以动态拆分并从费用映射中获取所有键的索引值?

mongodb aggregation-framework
1个回答
0
投票
  1. $set
    - 通过将
    charges
    对象的键值对转换为数组来设置
    charges
    字段,以便于迭代。

  2. $set
    - 设置
    charges
    字段。

    2.1。

    $map
    - 迭代
    charges
    数组的每个元素,并通过将字符串拆分为数组来替换
    v
    字段。

  3. $set
    - 设置
    charges
    字段。

    3.1。

    $map
    - 迭代
    charges
    数组的每个元素并返回一个新数组。

    3.1.1.

    $trim
    - 修剪输入字符串以删除(额外的)前面和多余的逗号字符。

    3.1.1.1。

    $reduce
    - 迭代数组的每个元素并转换以返回字符串。

    3.1.1.1.1。

    input
    - 使用
    $map
    运算符迭代数组索引的每个值,并通过
    $arrayElemAt
    按索引获取值。

    3.1.1.1.2。

    initialValue
    - 空字符串

    3.1.1.1.3。

    in
    - 通过
    $concat
    将累积的字符串值与当前迭代字符串值用“,”组合起来。

  4. $set
    - 通过将数组转换为键值对来设置
    charges
    字段。

db.collection.aggregate([
  {
    "$set": {
      "charges": {
        $objectToArray: "$charges"
      }
    }
  },
  {
    "$set": {
      "charges": {
        $map: {
          input: "$charges",
          in: {
            k: "$$this.k",
            v: {
              $split: [
                "$$this.v",
                ","
              ]
            }
          }
        }
      }
    }
  },
  {
    $set: {
      charges: {
        $map: {
          input: "$charges",
          in: {
            k: "$$this.k",
            v: {
              "$trim": {
                "input": {
                  $reduce: {
                    input: {
                      $map: {
                        input: /* Your array of index(s) */,
                        as: "i",
                        in: {
                          $arrayElemAt: [
                            "$$this.v",
                            "$$i"
                          ]
                        }
                      }
                    },
                    initialValue: "",
                    in: {
                      $concat: [
                        "$$value",
                        ",",
                        "$$this"
                      ]
                    }
                  }
                },
                "chars": ","
              }
            }
          }
        }
      }
    }
  },
  {
    $set: {
      charges: {
        $arrayToObject: "$charges"
      }
    }
  }
])

演示@Mongo Playground

© www.soinside.com 2019 - 2024. All rights reserved.