[英]How to $sum value on two collections in MongoDB?
我的英語不好,希望你能理解我的問題。 我是新手,我嘗試使用 2 個集合中的一些值。
我有兩個 collections:
集合 1:
{
_id: "61241ad82fcb9a369e59868a",
"name": "aaaa",
"products": [{
"_id": "611803232eeaf825336e26c4",
"name": "products1",
"pricePolicy": { "costPrice": 10 }
},
{
"_id": "611803232eeaf825336e26c5",
"name": "products2",
"pricePolicy": { "costPrice": 10 }
}]
},
{
_id: "61241ad82fcb9a369e59868b",
"name": "bbb",
"products": [{
"_id": "611803232eeaf825336e26c4",
"name": "products1",
"pricePolicy": { "costPrice": 20 }
},
{
"_id": "611803232eeaf825336e26c5",
"name": "products2",
"pricePolicy": { "costPrice": 10 }
}]
}
和集合 2:
{
"_id": "61179c8d4ef00f31df195223",
"name": "zzzz",
"listProduct": [
{
"id": "611803232eeaf825336e26c4",
"name": "products1",
"costPrice": 100,
},
{
"id": "611803232eeaf825336e26c6",
"name": "products3",
"costPrice": 10,
}
]
},
如何從兩個集合中 $sum costPrice
價值對產品進行排序? 結果如下:
{
_id: 611803232eeaf825336e26c4,
name: products1,
totalCollection1: 30,
totalCollection2: 10,
total: 40
}
{
_id: 611803232eeaf825336e26c5,
name: products2,
totalCollection1: 20,
totalCollection2: 0,
total: 20
}
{
_id: 611803232eeaf825336e26c6,
name: products3,
totalCollection1: 0,
totalCollection2: 10,
total: 10
}
我試過這個聚合查詢,它按預期工作。 我使用了你的問題帖子中的數據,結果在底部:
db.coll1.aggregate([
{ $unwind: "$products" },
{ $sort: { "products._id": 1 } },
{ $group: {
_id: "$products._id",
productName1: { $first: "$products.name" },
total1: { $sum: "$products.pricePolicy.costPrice" }
} },
{ $group: { _id: null, productSummary1: { $push: "$$ROOT" } } },
{ $lookup: {
from: "coll2",
pipeline: [],
as: "products2"
}},
{ $unwind: "$products2" },
{ $unwind: "$products2.listProduct" },
{ $sort: { "products2.listProduct.id": 1 } },
{ $group: {
_id: "$products2.listProduct.id",
productSummary1: { $first: "$productSummary1" },
productName2: { $first: "$products2.listProduct.name" },
total2: { $sum: "$products2.listProduct.costPrice" }
}},
{ $group: { _id: null,
productSummary1: { $first: "$productSummary1" },
productSummary2: { $push: { "_id": "$_id", "productName1": "$productName2", "total2": "$total2" } }
}},
{ $project: { products: { $concatArrays: [ "$productSummary1", "$productSummary2" ] } } },
{ $unwind: "$products" },
{ $group: {
_id: "$products._id",
productName: { $first: "$products.productName1" },
total1: { $sum: "$products.total1" },
total2: { $sum: "$products.total2" },
total1a: { $push: "$products.total1" },
total2a: { $push: "$products.total2" },
}},
{ $project: { productName: 1, total1: 1, total2: 1, total: { $sum: { $concatArrays: [ "$total1a", "$total2a" ] } } } },
{ $sort: { total: -1 } }
])
output:
{
"_id" : "611803232eeaf825336e26c4",
"productName" : "products1",
"total1" : 30,
"total2" : 100,
"total" : 130
}
{
"_id" : "611803232eeaf825336e26c5",
"productName" : "products2",
"total1" : 20,
"total2" : 0,
"total" : 20
}
{
"_id" : "611803232eeaf825336e26c6",
"productName" : "products3",
"total1" : 0,
"total2" : 10,
"total" : 10
}
$unwind
解構products
數組$group
by null 並構造具有必填字段的products
數組$lookup
集合 2
$unwind
解構listProduct
數組$project
顯示必填字段$project
在單個字段products
中連接兩個 collections 數組$unwind
解構上面的products
數組$group
by _id
表示產品 ID 並獲得兩個集合的價格總和$ifNull
檢查值是否為 null 然后它將返回 0$sort
按總數降序排序db.col1.aggregate([
{ $unwind: "$products" },
{
$group: {
_id: null,
col1: {
$push: {
_id: "$products._id",
name: "$products.name",
totalCollection1: "$products.pricePolicy.costPrice"
}
}
}
},
{
$lookup: {
from: "col2",
pipeline: [
{ $unwind: "$listProduct" },
{
$project: {
_id: "$listProduct.id",
name: "$listProduct.name",
totalCollection2: "$listProduct.costPrice"
}
}
],
as: "col2"
}
},
{ $project: { products: { $concatArrays: ["$col1", "$col2"] } } },
{ $unwind: "$products" },
{
$group: {
_id: "$products._id",
name: { $first: "$products.name" },
totalCollection1: { $sum: { $ifNull: ["$products.totalCollection1", 0] } },
totalCollection2: { $sum: { $ifNull: ["$products.totalCollection2", 0] } }
}
},
{ $addFields: { total: { $sum: ["$totalCollection1", "$totalCollection2"] } } },
{ $sort: { total: -1 } }
])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.