将对象数组内的concat int数组字段转换为mongodb聚合中的一个字符串字段

时间:2020-04-12 13:20:36

标签: mongodb type-conversion aggregate string-concatenation

我想在将对象数组中的int数组字段值除以10后将其合并为一个字符串字段。

此处使用现有文档格式:

{ 
  "no" : "2020921008981",  
  "date" : ISODate("2020-04-01T05:19:02.263+0000"), 
  "sale" : { 
   "soldItems" : [
       {
         "itemRefId" : "5b55ac7f0550de00210a3b24", 
         "soldPrice" : NumberInt(800), 
       },
       {
         "itemRefId" : "5b55ac7f0550de00210a3b25", 
         "soldPrice" : NumberInt(1000), 
       }
     ] 
   }
 }

预期结果:

{ 
  "no" : "2020921008981",  
  "date" : ISODate("2020-04-01T05:19:02.263+0000"),  
  "priceList" : "8.0 \n 10.0"
}

使用$ reduce的尝试:

 priceList: {
            $reduce: {
                input: "$sale.soldItems.soldPrice",
                initialValue: "",
                in: {
                    $cond: [ { "$eq": [ { $toString: { $divide: [ "$$value", 10 ] } }, "" ] }, "$$this", { $concat: [ { $toString: { $divide: [ "$$value", 10 ] } }, "\n", "$$this" ] } ]
                }
            }
        }

但是最终会收到"errmsg" : "$divide only supports numeric types, not string and double"错误。任何想法将不胜感激。

2 个答案:

答案 0 :(得分:0)

请尝试以下聚合查询,其目的是:

  • 首先将字段soldPrice除以10或使用$divide除以所需的除数
  • 使用$toString$concat
  • 将其转换为字符串和concat
  • 追加器\n在每个reduce op之后被追加,使用$rtrim从头删除该追加器
  • 使用$addFields
  • 创建新字段

查询:

db.collection.aggregate([
  {
    $addFields: {
      "itemPriceList": {
        $rtrim: {
          input: {
            $reduce: {
              input: "$salesOrder.purchaseItems",
              initialValue: "",
              in: {
                $concat: [
                  "$$value",
                  {
                    $toString: {
                      $divide: [
                        "$$this.soldPrice",
                        10
                      ]
                    }
                  },
                  "\n"
                ]
              }
            }
          },
          chars: "\n"
        }
      }
    }
  }
]);

结果:

[
  {
    "_id": ObjectId("5a934e000102030405000000"),
    "caseNumber": "2020921008981",
    "itemPriceList": "80\n100",
    "salesOrder": {
      "purchaseItems": [
        {
          "itemRefId": "5b55ac7f0550de00210a3b24",
          "soldPrice": 800
        },
        {
          "itemRefId": "5b55ac7f0550de00210a3b25",
          "soldPrice": 1000
        }
      ]
    },
    "startTime": ISODate("2016-05-18T16:00:00Z")
  }
]

Plaground Test Link

答案 1 :(得分:0)

db.case.aggregate([
    {
        $set: {
            priceList: {
                $reduce: {
                    input: {
                        $map: {
                            input: "$sale.soldItems.soldPrice",
                            in: { $toString: { $divide: ["$$this", 10] } }
                        }
                    },
                    initialValue: "",
                    in: { $concat: ["$$value", "$$this", " \n "] }
                }
            }
        }
    },
    {
        $project: {
            _id: 0,
            no: 1,
            date: 1,
            priceList: 1
        }
    }
])