簡體   English   中英

mongodb nodej 驅動程序中的 $sum 聚合

[英]$sum aggregation in mongodb nodej driver

集合 1:管理員

{
    "_id" : ObjectId("5e27fd3da42d441fe8a89580"),
    "mappedcustomers": [
        ObjectId("5e2555783405363bc4bf86c5"),
        ObjectId("5e2555783405363bc4bf86c0"),
        ObjectId("5e2555783405363bc4bf86c4")
    ],
    "phoneNo" : 9897654358,
    "name" : "acdbcs"
}

集合 2:productOrders

Tt有很多文件,我們只關心:

  1. "productOrderedForDate" : ISODate("2020-02-04T18:30:00Z") [明天的訂單]。
  2. "productOrderedForDate" : ISODate("2020-02-28T18:30:00Z") [上周訂單]
[
    {
        "_id": ObjectId("5e27f998a42d441fe8a8957f"),
        "authorized": false,
        "orderCreatedBy": ObjectId("5e2555783405363bc4bf86c4"), // one of the mappedCustomer
        "productOrderedForDate": ISODate("2020-02-04T18:30:00Z"),// tomorrow Order
        "order": [{
            "_id": ObjectId("5e26be2cc13b7149d0a95110"),
            "productName": "Cups",
            "productCode": "CICE1",
            "size R": 21,
            "size L": 16
        },
            {
                "_id": ObjectId("5e26be2cc13b7149d0a9510f"),
                "productName": "Bottles",
                "productCode": "BTCE1",
                "size R": 12,
                "size L": 3
            }]
    },
    {
        "_id": ObjectId("5e26be2cc13b7149d0b90752b"),
        "authorized": false,
        "orderCreatedBy": ObjectId("5e2555783405363bc4bf86c0"),// another mappedCustomer
        "productOrderedForDate": ISODate("2020-02-04T18:30:00Z"),// tomorrow Order
        "order": [{
            "_id": ObjectId("5e26be2cc13b7149d0a87230"),
            "productName": "Cups",
            "productCode": "CICE1",
            "size R": 9,
            "size L": 7
        },
            {
                "_id": ObjectId("5e26be2cc13b7149d0a8560e"),
                "productName": "Bottles",
                "productCode": "BTCE1",
                "size R": 3,
                "size L": 11
            }]

    },
    {
        "_id": ObjectId("5e26be2cc13b7149d0b9876f"),
        "authorized": true,
        "orderCreatedBy": ObjectId("5e2555783405363bc4bf86c4"), // one of the mappedCustomer
        "productOrderedForDate": ISODate("2020-01-28T18:30:00Z"),// lastWeek order 
        "order": [{
            "_id": ObjectId("5e26be2cc13b7149d0a54220"),
            "productName": "Cups",
            "productCode": "CICE1",
            "size R": 2,
            "size L": 6
        },
            {
                "_id": ObjectId("5e26be2cc13b7149d0a6520e"),
                "productName": "Bottles",
                "productCode": "BTCE1",
                "size R": 8,
                "size L": 16
            }]

    },
    {

        "_id": ObjectId("5e78f998a42d441fe898765d"),
        "authorized": true,
        "orderCreatedBy": ObjectId("5e2555783405363bc4bf86c0"), // another mappedCustomer
        "productOrderedForDate": ISODate("2020-01-28T18:30:00Z"),// lastWeek order 
        "order": [{
            "_id": ObjectId("5e26be2cc13b7149d0a87230"),
            "productName": "Cups",
            "productCode": "CICE1",
            "size R": 26,
            "size L": 19
        },
            {
                "_id": ObjectId("5e26be2cc13b7149d0a8560f"),
                "productName": "Bottles",
                "productCode": "BTCE1",
                "size R": 4,
                "size L": 5
            }]
    }
]

這是我嘗試過的並且能夠展開所有映射的客戶,因此我能夠在下面的訂單集合中找到它們創建的訂單是聚合管道

db.admin.aggregate([
    {
        $match: {
            _id: ObjectId("5e27fd3da42d441fe8a89580")
        }
    },
    {
        $lookup:
            {
                from: 'admin',
                localField: 'mappedCustomers',
                foreignField: '_id',
                as: 'mappedCustomers'
            }
    },
    {
        $unwind: '$mappedCustomers'
    },
    {
        $replaceRoot: {newRoot: "$mappedCustomers"}
    },
    {
        $lookup:
            {
                from: "orders",
                let: {mappedCustomersId: "$_id"},
                pipeline: [
                    {
                        $match: {
                            $expr: {$eq: ["$orderCreatedBy", "$$mappedCustomersId"]},
                            '$or': [
                                {
                                    'orderCreatedOn': ISODate("2020-02-04T18:30:00Z")
                                }, {
                                    'orderCreatedOn': ISODate("2020-01-28T18:30:00Z")
                                }]
                        }
                    }],
                as: "orders"
            }
    }, {
        $unwind: "orders"
    }
])

我的問題是,我需要針對明天日期和上周日期在該管理員下映射的productCode所有映射的mappedCustomers的所有size Rsize L的總和,即

預期輸出:

{
    orders : [
        {
            "productOrderedForDate": ISODate("2020-02-04T18:30:00Z"),
            "productName": "Cups",
            "productCode": "CICE1",
            "size R": 30,
            "size L": 23,
            "lastWeek": [{
                "productOrderedForDate": ISODate("2020-01-28T18:30:00Z"),
                "size R": 28,
                "size L": 25,
            }]
        }, {
            "productOrderedForDate": ISODate("2020-02-04T18:30:00Z"),
            "productName": "Bottles",
            "productCode": "BTCE1",
            "size R": 15,
            "size L": 14,
            "lastWeek": [{
                "productOrderedForDate": ISODate("2020-01-28T18:30:00Z"),
                "size R": 12,
                "size L": 21,
            }]
        }
    ]
}

回顧一下: 1. 我將從req.body獲取管理員id 2. 我會發現所有的客戶都映射到了mappedCustomers 3. 我將從orders集合中查找mappedCustomers為所需日期創建的訂單。 4. 我需要將所有size Rsize L分組。

我設法做到了 1,2,3,但我無法為 4 和 5 產生所需的結果。請看看它並告訴我這是否可以實現。

我已經看過這篇文章,但我無法讓它工作。

嘗試這個:

db.admin.aggregate([
  {
    $match: {
      _id: ObjectId("5e27fd3da42d441fe8a89580")
    }
  },
  {
    $lookup: {
      from: "admin",
      localField: "mappedcustomers",
      foreignField: "_id",
      as: "mappedcustomers"
    }
  },
  {
    $unwind: "$mappedcustomers"
  },
  {
    $replaceRoot: {
      newRoot: "$mappedcustomers"
    }
  },
  {
    $lookup: {
      from: "orders",
      let: {
        mappedCustomersId: "$_id"
      },
      pipeline: [
        {
          $match: {
            $expr: {
              $eq: [
                "$orderCreatedBy",
                "$$mappedCustomersId"
              ]
            },
            "$or": [
              {
                "productOrderedForDate": ISODate("2020-02-04T18:30:00Z")
              },
              {
                "productOrderedForDate": ISODate("2020-01-28T18:30:00Z")
              }
            ]
          }
        }
      ],
      as: "orders"
    }
  },
  {
    $unwind: "$orders"
  },
  {
    $unwind: "$orders.order"
  },
  {
    $group: {
      _id: "$orders.order.productCode",
      orders: {
        $push: {
          productOrderedForDate: "$orders.productOrderedForDate",
          productName: "$orders.order.productName",
          productCode: "$orders.order.productCode",
          "size R": "$orders.order.size R",
          "size L": "$orders.order.size L"
        }
      }
    }
  },
  {
    $project: {
      thisweek: {
        $reduce: {
          input: {
            $filter: {
              input: "$orders",
              cond: {
                $eq: [
                  "$$this.productOrderedForDate",
                  ISODate("2020-02-04T18:30:00Z")
                ]
              }
            }
          },
          initialValue: {
            "size R": 0,
            "size L": 0
          },
          in: {
            productOrderedForDate: "$$this.productOrderedForDate",
            "productName": "$$this.productName",
            "productCode": "$$this.productCode",
            "size R": {
              $add: [
                "$$value.size R",
                "$$this.size R"
              ]
            },
            "size L": {
              $add: [
                "$$value.size L",
                "$$this.size L"
              ]
            }
          }
        }
      },
      lastWeek: {
        $reduce: {
          input: {
            $filter: {
              input: "$orders",
              cond: {
                $eq: [
                  "$$this.productOrderedForDate",
                  ISODate("2020-01-28T18:30:00Z")
                ]
              }
            }
          },
          initialValue: {
            "size R": 0,
            "size L": 0
          },
          in: {
            productOrderedForDate: "$$this.productOrderedForDate",
            "size R": {
              $add: [
                "$$value.size R",
                "$$this.size R"
              ]
            },
            "size L": {
              $add: [
                "$$value.size L",
                "$$this.size L"
              ]
            }
          }
        }
      }
    }
  },
  {
    $group: {
      _id: null,
      orders: {
        $push: {
          $mergeObjects: [
            "$thisweek",
            {
              "lastWeek": [
                "$lastWeek"
              ]
            }
          ]
        }
      }
    }
  },
  {
    $unset: "_id"
  }
])

蒙戈游樂場

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM