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

Arrays 如何使用MongoDB将对象值的嵌套数组转换为逗号分隔的字符串,arrays,mongodb,mongodb-query,aggregation-framework,Arrays,Mongodb,Mongodb Query,Aggregation Framework,我在使用MongoDb的文档中有一些嵌套的对象数组。我在下面解释我的文件 { "Products" : [ { "ID" : 0, "StoreCode" : "CMHB", "StoreName" : "CMR NPS", "StoreDescription" : "CMR NPS International Store is a place where Parents ca

我在使用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()

在这里,我需要再添加一个键
,即e-SKU
,其值应该是产品数组中所有SKU值的逗号分隔字符串,如
,例如-sku1、sku2、sku3……
,您需要添加这些更改:

您的
$group
阶段需要:

SKU: { $push: "$Products.SKU"} // Since SKU is in Products array, final SKU will be array of arrays [[123,456], [789]]
添加新阶段
$addFields

{
      $addFields: {
        SKU: {
          $substr: [
            {
              "$reduce": {
                "input": { "$reduce": { "input": "$SKU", "initialValue": [], "in": { "$setUnion": [ "$$value", "$$this" ] } } }, // Merge array of arrays to one array
                "initialValue": "",
                "in": { "$concat": [ "$$value", ",", "$$this" ] } // Concat array of strings to one string & remove first char from final string which is `,`
              }
            },
            1,
           -1
         ]
       }
     }
  }
测试:

Ref: