mongodb nodej驱动程序中的$ sum聚合

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

集合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
            }]
    }
]

这是我尝试过的并且能够解散所有mapedCustomers,并且据此,我已经能够在下面的订单集合中找到由他们创建的订单是聚合管道

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"
    }
])

我的问题是,我需要针对明天的日期和上一周的日期(即,该日期)显示该管理员下映射的size R的所有size L的所有mappedCustomersproductCode之和

预期输出:

{
    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.我将从id中获取管理员req.body。2.我将找到映射到mappedCustomers的所有客户。3.我将从orders集合中查找mappedCustomers为所需日期创建的订单。4.我需要将所有size Rsize L分组。

我设法完成了1,2,3,但无法产生4和5的所需结果。请查看一下,并告诉我是否可以实现。

我已经看过this帖子,但无法正常工作。

node.js mongodb mongoose aggregation-framework
1个回答
0
投票
尝试一下:

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" } ])

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