Mongodb-每个对象的汇总和总和

时间:2019-09-09 14:01:56

标签: mongodb mongodb-query

我需要对销售数据库中的每个“产品”对象进行汇总,并对“价格”和“数量”求和,以进行产品分类。

我能够按“产品”分组,但是我无法将每个对象的价格和数量求和。

{$group: {
     _id: '$products.items',
     totalprice:{$sum: "$products.items.price"},
}}

下面的销售数据库样本中,我需要返回为每个“产品”出售的“价格”和“数量”字段的总和。

{
    "_id": {
        "$oid": "5d753707c0cd851e80da914c"
    },
    "created_on": {
        "$date": {
            "$numberLong": "1567962886000"
        }
    },
    "custumer": {
        "name": "Teste",
        "cep": "teste",
        "address": "teste",
        "district": "test",
        "city": "test",
        "numb": "50",
        "comple": "test",
        "state": "test",
        "cpf": "test",
        "birth": "30/09/1977",
        "email": "test@gmail.com",
        "phone": {
            "$numberDouble": "1111111111111"
        },
        "gender": "M",
        "portalt": {
            "status": "true",
            "vendor": "test",
            "phone": {
                "$numberDouble": "11111111111"
            },
            "sim": "011111111111",
            "salesnumb": "1222222222222222222"
        }
    },
    "payment": {
        "method": "Boleto",
        "type": "Parcelado",
        "installments": "5",
        "billing_date": "15"
    },
    **"products": {
        "items": {
            "5d515979736802000415a561": {
                "item": {
                    "_id": "5d515979736802000415a561",
                    "name_produto": "Product 1",
                    "resumo": "Minutos ilimitados,20GB + 2GB",
                    "price": "110",
                    "_image": "images/test.jpg"
                },
                "quantity": {
                    "$numberInt": "2"
                },
                "price": {
                    "$numberInt": "220"
                }
            },
            "5d515aba736802000415a562": {
                "item": {
                    "_id": "5d515aba736802000415a562",
                    "name_produto": "Product 2",
                    "resumo": "Minutos ilimitados,3GB + 1GB",
                    "price": "80",
                    "_image": "images/test.jpg"
                },
                "quantity": {
                    "$numberInt": "1"
                },
                "price": {
                    "$numberInt": "80"
                }
            },
            "5d515dbf736802000415a564": {
                "item": {
                    "_id": "5d515dbf736802000415a564",
                    "name_produto": "Product 3",
                    "resumo": "Minutos ilimitados,30GB + 3GB",
                    "price": "150",
                    "_image": "images/test.jpg"
                },
                "quantity": {
                    "$numberInt": "1"
                },
                "price": {
                    "$numberInt": "150"
                }
            }
        },**
        "totalItems": {
            "$numberInt": "4"
        },
        "totalPrice": {
            "$numberInt": "450"
        }
    },
    "seller": {
        "_id": {
            "$oid": "5cd086787dc59921bcad94d8"
        },
        "name": "test"
    }
}

我需要输出如下内容:

_id:Object
    5d515979736802000415a561:{sum_price: 300, sum_quantity: 30 }
    5d515aba736802000415a562:{sum_price: 500, sum_quantity: 60 }
    5d515dbf736802000415a564:{sum_price: 600, sum_quantity: 70 }

非常感谢!

1 个答案:

答案 0 :(得分:0)

因此,让我们与OP一起对各个产品的价格和数量求和。除去与询问无关的其他字段,我们得出这样的结果:

var r =
[
 {
     _id:0,
     "products": {
        "items": {
            "5d515979736802000415a561": {
                "quantity": 2,
                "price": 220,
            },
            "5d515aba736802000415a562": {
                "quantity": 1,
                "price": 80
            }
        }
     }
 }
 , {
     _id:1,
     "products": {
        "items": {
            "5d515979736802000415a561": {  // deliberately same id as doc above but different quantity and price
                "quantity": 3,
                "price": 330
            },
            "5d515aba736802000415a562": { // same
                "quantity": 2,
                "price": 160
            },
            "5d515979736802000415ZZZZ": {  // different than above; adds "third item"
                "quantity": 4,
                "price": 200
            }
        }
     }
 }
 ];

请注意,整个item内部字段基本上并不重要,至少其中一个字段仅包含单价,而不包含总价格(数量)和每种产品的数量。

            "5d515dbf736802000415a564": {
                "item": {
                    "_id": "5d515dbf736802000415a564",
                    // etc

因此,现在我们使用$objectToArray将密钥转换为rval。这为我们提供了$group可以使用的功能,因此这里是一个解决方案:

db.foo.aggregate([
{$project: {X: {$objectToArray: "$products.items"}}}
,{$unwind: "$X"}
,{$group: {_id: "$X.k", tot_q: {$sum:"$X.v.quantity"}, tot_amt: {$sum:"$X.v.price"}} }
                      ]);

上面给出的输入产生了:

{ "_id" : "5d515979736802000415ZZZZ", "tot_q" : 4, "tot_amt" : 200 }
{ "_id" : "5d515aba736802000415a562", "tot_q" : 3, "tot_amt" : 240 }
{ "_id" : "5d515979736802000415a561", "tot_q" : 5, "tot_amt" : 550 }