簡體   English   中英

如何對 MongoDB 中的兩個 collections 求和?

[英]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
  • `$addFields 添加新字段 total 以獲得 collections 價格的總和
  • $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.

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