由供应商名称分组

发布于 2025-02-10 19:30:59 字数 3244 浏览 1 评论 0 原文

我有一个带有商店订单(“从客户”和“到供应商”)的集合:

[{
  "orderNo": 1000,
  "orderItem": 0,
  "orderType": "CUST",
  "company": {
    "name": "CUSTOMER GMBH",
    "uic": "DE300000XX"
  },
  "custPO": {
    "grandTotal_eur": 146,
    "products": [
      {
        "product": {
          "code": "PROD-1000",
          "title": "Product title 1000",
          "toBuy": true,
          "supplier": {
            "code": "PROD-SUPP-1000",
            "title": "Product supplier title 1000",
            "company": {
              "name": "SUPPLIER ONE GMBH",
              "uic": "DE1000XXXX"
            },
            "price": 6
          }
        },
        "qty": 5,
        "price": 10,
        "valueTotal": 50
      },
      {
        "product": {
          "code": "PROD-2000",
          "title": "Product title 2000",
          "toBuy": true,
          "supplier": {
            "code": "PROD-SUPP-2000",
            "title": "Product supplier title 2000",
            "company": {
              "name": "SUPPLIER TWO GMBH",
              "uic": "DE2000XXXX"
            },
            "price": 15
            }
        },
        "qty": 2,
        "price": 20,
        "valueTotal": 40
      },
      {
        "product": {
            "code": "PROD-1010",
            "title": "Product title 1010",
            "toBuy": true,
            "supplier": {
              "code": "PROD-SUPP-1010",
              "title": "Product supplier title 1010",
              "company": {
                "name": "SUPPLIER ONE GMBH",
                "uic": "DE1000XXXX"
              },
              "price": 2 
            }
        },
        "qty": 2,
        "price": 3,
        "valueTotal": 6
      },
      {
        "product": {
          "code": "TRANS",
          "title": "Transport fees"
        },
        "qty": 1,
        "price": 50,
        "valueTotal": 50
      }
    ]
  }
},
{
  "orderNo": 1000,
  "orderItem": 1,
  "orderType": "SUPP",
  "company": {
    "name": "SUPPLIER ONE GMBH",
    "uic": "DE1000XXXX"
  },
  "suppPO": {
    "grandTotal_eur": 34,
    "products": [
      {
        "product": {
          "code": "PROD-SUPP-1000",
          "title": "Product supplier title 1000"
          },
        "qty": 5,
        "price": 6,
        "valueTotal": 30
      },
      {
        "product": {
            "code": "PROD-SUPP-1010",
            "title": "Product supplier title 1010"
        },
        "qty": 2,
        "price": 2,
        "valueTotal": 4
      }
    ]
  }
},
{
...
}]

总的来说,根据购物清单中产品的每个客户订单(例如1000-0)随后将其转换为多个供应商订单(例如1000-1,1000-2)。

我需要做的是创建一个mongoDB汇总,该汇总为 sum valueTotal 的所有产品标记为 tobuy ,由 >供应商(供应商由custpo.products。$。product.supplier.company.name

结束时,结果应该是这样的:

[{
  "orderNo": 1000,
  "company": {
    "name": "CUSTOMER GMBH",
    "uic": "DE300000XX"
  },
  "totals": [{
    "supplierName": "SUPPLIER ONE GMBH",
    "supplierTotal": 56
  },{
    "supplierName": "SUPPLIER TWO GMBH",
    "supplierTotal": 40
  },{
    "supplierName": null,
    "supplierTotal": 50
  }]
}]

可以调整输出的结构根据可能性,重要的是触摸总和名称。

如果可以用一个聚集体进行一些提示或解决方案,请为我提供一些提示或解决方案。 使用了最新版本。

I have a collection with store orders ("from customers" and "to suppliers") having this structure:

[{
  "orderNo": 1000,
  "orderItem": 0,
  "orderType": "CUST",
  "company": {
    "name": "CUSTOMER GMBH",
    "uic": "DE300000XX"
  },
  "custPO": {
    "grandTotal_eur": 146,
    "products": [
      {
        "product": {
          "code": "PROD-1000",
          "title": "Product title 1000",
          "toBuy": true,
          "supplier": {
            "code": "PROD-SUPP-1000",
            "title": "Product supplier title 1000",
            "company": {
              "name": "SUPPLIER ONE GMBH",
              "uic": "DE1000XXXX"
            },
            "price": 6
          }
        },
        "qty": 5,
        "price": 10,
        "valueTotal": 50
      },
      {
        "product": {
          "code": "PROD-2000",
          "title": "Product title 2000",
          "toBuy": true,
          "supplier": {
            "code": "PROD-SUPP-2000",
            "title": "Product supplier title 2000",
            "company": {
              "name": "SUPPLIER TWO GMBH",
              "uic": "DE2000XXXX"
            },
            "price": 15
            }
        },
        "qty": 2,
        "price": 20,
        "valueTotal": 40
      },
      {
        "product": {
            "code": "PROD-1010",
            "title": "Product title 1010",
            "toBuy": true,
            "supplier": {
              "code": "PROD-SUPP-1010",
              "title": "Product supplier title 1010",
              "company": {
                "name": "SUPPLIER ONE GMBH",
                "uic": "DE1000XXXX"
              },
              "price": 2 
            }
        },
        "qty": 2,
        "price": 3,
        "valueTotal": 6
      },
      {
        "product": {
          "code": "TRANS",
          "title": "Transport fees"
        },
        "qty": 1,
        "price": 50,
        "valueTotal": 50
      }
    ]
  }
},
{
  "orderNo": 1000,
  "orderItem": 1,
  "orderType": "SUPP",
  "company": {
    "name": "SUPPLIER ONE GMBH",
    "uic": "DE1000XXXX"
  },
  "suppPO": {
    "grandTotal_eur": 34,
    "products": [
      {
        "product": {
          "code": "PROD-SUPP-1000",
          "title": "Product supplier title 1000"
          },
        "qty": 5,
        "price": 6,
        "valueTotal": 30
      },
      {
        "product": {
            "code": "PROD-SUPP-1010",
            "title": "Product supplier title 1010"
        },
        "qty": 2,
        "price": 2,
        "valueTotal": 4
      }
    ]
  }
},
{
...
}]

In general, each customer order (eg. 1000-0), based on the products from the shopping list, is later transposed to multiple supplier orders (eg. 1000-1, 1000-2).

What I need to do, is to create a MongoDB aggregate that output the sum of valueTotal for all products marked as toBuy, grouped by supplier (supplier is represented by custPO.products.$.product.supplier.company.name)

At the end the result should be something like this:

[{
  "orderNo": 1000,
  "company": {
    "name": "CUSTOMER GMBH",
    "uic": "DE300000XX"
  },
  "totals": [{
    "supplierName": "SUPPLIER ONE GMBH",
    "supplierTotal": 56
  },{
    "supplierName": "SUPPLIER TWO GMBH",
    "supplierTotal": 40
  },{
    "supplierName": null,
    "supplierTotal": 50
  }]
}]

Structure of the output can be adjusted based on the possibilities, important is to touch the sum and supplier name.

Please help me with some hints or a solution, if it is possible to do it with one aggregate.
Latest versions are used.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鞋纸虽美,但不合脚ㄋ〞 2025-02-17 19:30:59
  1. $ undind - 解构 custpo.products 到多个文档的数组。

  2. $ match - 用过滤文档“ custpo.products.product.tobuy”:true

  3. $ group - 组成 orderno supplierName

    3.1。获取公司通过 $ first

    3.2。 sum custpo.products.valuetotal 作为 supplototal

  4. $ group - 组成 orderno

    4.1。获取公司通过 $ first

    4.2。用 supplierName supplototal 将文档推入 totals array。

db.collection.aggregate([
  {
    $unwind: "$custPO.products"
  },
  {
    $match: {
      "custPO.products.product.toBuy": true
    }
  },
  {
    $group: {
      _id: {
        orderNo: "$orderNo",
        supplierName: "$custPO.products.product.supplier.company.name"
      },
      company: {
        $first: "$company"
      },
      "supplierTotal": {
        $sum: "$custPO.products.valueTotal"
      }
    }
  },
  {
    $group: {
      _id: "$_id.orderNo",
      company: {
        $first: "$company"
      },
      "totals": {
        $push: {
          "supplierName": "$_id.supplierName",
          "supplierTotal": "$supplierTotal"
        }
      }
    }
  }
])

示例mongo playground

  1. $unwind - Deconstruct custPO.products array to multiple documents.

  2. $match - Filter the documents with "custPO.products.product.toBuy": true.

  3. $group - Group by orderNo and supplierName.

    3.1. Get company via $first.

    3.2. Sum custPO.products.valueTotal as supplierTotal.

  4. $group - Group by orderNo.

    4.1. Get company via $first.

    4.2. Push the documents with supplierName and supplierTotal into totals array.

db.collection.aggregate([
  {
    $unwind: "$custPO.products"
  },
  {
    $match: {
      "custPO.products.product.toBuy": true
    }
  },
  {
    $group: {
      _id: {
        orderNo: "$orderNo",
        supplierName: "$custPO.products.product.supplier.company.name"
      },
      company: {
        $first: "$company"
      },
      "supplierTotal": {
        $sum: "$custPO.products.valueTotal"
      }
    }
  },
  {
    $group: {
      _id: "$_id.orderNo",
      company: {
        $first: "$company"
      },
      "totals": {
        $push: {
          "supplierName": "$_id.supplierName",
          "supplierTotal": "$supplierTotal"
        }
      }
    }
  }
])

Sample Mongo Playground

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文