2
我是MongoDB的新手,如果我錯過了文檔中的某些內容,請原諒我。我的收藏是這樣的:
{
"_id" : ObjectId("57553e7015e4117a4343c18c"),
"BuyingPrice" : 55.5,
"Quantity" : NumberLong(1),,
"Brand" : "Ranamina",
"Amount" : 79.99,
"Profit" : 24.49,
"ProductId" : NumberLong(55319),}
每一個品牌的計算總量後,我想看看有多少產品在每一個品牌。
我希望看到的結果
{"Quantity": 1982,
"Amount": 155,
"Number_of_product" :12,
"_id":{"Brand": "Ranamina"}
}
這是我的代碼:
collection_test.aggregate([
{
'$group': {
'_id': {
'Brand': '$Brand',
},
'Amount': {'$sum': '$Amount'},
'product': {'$addToSet': '$ProductId'},
'Amount_sum': {'$addToSet': '$Amount'},
},
},
{'$group': {
'_id': {'Brand': '$_id.Brand', 'ProductId': '$product'},
}
},
{
'$project': {'count': {'$size': '$_id.ProductId'},
'Groups': '$_id.Groups', '_id': 0, 'Amount_sum':1,
}}
])
我使用的組羣中的方法,但是我想不出訪問總量和利潤總額。有沒有辦法訪問這些?
非常感謝你!有用。 – Lutfiye