如何使用MongoDB将对象值的嵌套数组转换为逗号分隔的字符串

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

我使用MongoDb在文档中有一些嵌套的对象数组。我在下面解释我的文档。

{
  "Products" : [ 
        {
            "ID" : 0,
            "StoreCode" : "CMHB",
            "StoreName" : "CMR NPS",
            "StoreDescription" : "CMR NPS  International Store is a place where Parents can purchase all the school merchandise in one place at reasonable prices.",
            "StoreBranch" : "HRBR Layout",
            "AddressLine1" : "HRBR Layout",
            "AddressLine2" : "HRBR Layout",
            "Street" : "",
            "Landmark" : "",
            "Latitude" : "0",
            "Longitude" : "0",
            "City" : "Bengaluru",
            "State" : "Karnataka",
            "Country" : "India",
            "Pincode" : "560043",
            "StoreType" : "Online",
            "WarehouseCode" : "D0001",
            "Description" : "",
            "ProductName" : "Grade-12 Commerce EABM Book Kit",
            "ProductCode" : "VNTKEPCBM",
            "ProductType" : "S",
            "Brand" : "EP",
            "VendorCode" : "",
            "SKU" : "CMHBVNTKEPCBM",
            "CONSKU" : "",
            "BASESKU" : "",
            "PARENTSKU" : "",
            "SubProducts" : [],
            "CategoryId" : "",
            "CategoryName" : "Books/Kit",
            "AttributeSet" : "4000000",
            "Gender" : "",
            "BaseUnitPrice" : 0,
            "TaxPercentage" : 0,
            "HSNCode" : 4901,
            "BaseUnitOfMeasure" : "",
            "Weight" : 0,
            "TaxAmount" : 0,
            "MRP" : 0,
            "Weightage" : "",
            "DiscountPrice" : 0,
            "TotalOrderQuantity" : 1,
            "TotalProductPrice" : 0,
            "TotalProductDiscountPrice" : 0,
            "MinimumPrice" : 0,
            "CurrencyCode" : "INR",
            "MinimumBuyQty" : 1,
            "MaximumBuyQty" : 1,
            "TotalBaseUnitofMeasure" : "",
            "TotalWeight" : ""
        }
    ],
}

所以这是一个示例记录,我的产品数组具有一个键名SKU。我需要用逗号分隔的字符串连接所有SKU值。让我在下面解释我现有的查询。

db.getCollection('orders').aggregate([
      {
          $match: {"Customer.StoreCode":"CMHB"}
      },
      {
          $group: {
              _id : "$Customer.CustomerMobile", 
              "data" : {
                  "$push": {
                     OrderNumber:"$OrderNumber",
                    OrderStatus:"$OrderStatus",
                    OrderType:"$OrderType",
                    CreatedAt:{ $dateToString: { format: "%Y-%m-%d", date: "$CreatedAt" } },
                    CustomerMobile: "$Customer.CustomerMobile",
                    CustomerLastName:"$Customer.CustomerLastName",
                    CustomerFirstName:"$Customer.CustomerFirstName",
                    StoreCode:"$Customer.StoreCode",
                    TransactionId:"$PaymentDetails.TransactionId",
                    PaymentStatus:"$PaymentDetails.PaymentStatus",
                    PaymentAmount:"$PaymentDetails.PaymentAmount",
                    ItemNos: { $cond: { if: { $isArray: "$Products" }, then: { $size: "$Products" }, else: "NA"} },
                    BillingAddressesLine1: "$Customer.BillingDetails.BillingAddressesLine1",
                    BillingAddressesLine2: "$Customer.BillingDetails.BillingAddressesLine2"
                  }
              }
          }
      }
]).toArray()

[这里我需要再添加一个键i.e-SKU,它的值应为逗号分隔的产品数组中所有sku值的字符串,例如e.g-sku1,sku2,sku3....

arrays mongodb aggregate
1个回答
0
投票

您需要添加以下更改:

您的$group阶段需要此:

SKU: { $push: "$Products.SKU"}

添加新阶段$addFields

{
      $addFields: {
        SKU: {
          $substr: [
            {
              "$reduce": {
                "input": { "$reduce": { "input": "$SKU", "initialValue": [], "in": { "$setUnion": [ "$$value", "$$this" ] } } },
                "initialValue": "",
                "in": { "$concat": [ "$$value", ",", "$$this" ] }
              }
            },
            2,
           -1
         ]
       }
     }
  }

Test: mongoplayground

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