2016-06-24 88 views
2

計算領域我有這樣如何在MongoDB中聚集

{ 
    "_id": ObjectId("5644c495d0807a1750043237"), 
    "siteid": "123456" 
    "amount": 1.32 
} 

有些文件有其他款項如與項目的文件。 "cashbackAmount"

我想要一個sum和一個count爲每個數量字段。並非每個文檔都包含所有金額字段。

我hjave嘗試以下

{ 
    $group: { 
     "_id": "$siteid", 
     item2: { "$sum": "$amount" }, 
     item3: { "$sum": "$totalAmount" }, 
     item4: { "$sum": "$cashbackAmount" }, 
     item5: { "$sum": "$unitPrice" }, 
    } 
} 

它給我的總和,但我不能工作了如何讓數次,每次量字段存在。

{ "$sum": 1 }不起作用,因爲這給了我具有任何一個總計字段的所有文檔。

+0

你是什麼意思'獲取每個數量字段存在的數字次數?你能給出一些預期的輸出例子嗎? – Shrabanee

回答

1

我猜你可能灣t類似的東西

db.getCollection('amounts').aggregate([ 
    { 
     $project: { 
      siteid: 1, 
      amount: 1, 
      totalAmount: 1, 
      unitPrice: 1, 
      cashbackAmount: 1, 
      amountPresent: { 
       $cond: { 
        if: "$amount", 
        then: 1, 
        else: 0 
       } 
      }, 
      totalAmountPresent: { 
       $cond: { 
        if: "$totalAmount", 
        then: 1, 
        else: 0 
       } 
      }, 
      cashbackAmountPresent: { 
       $cond: { 
        if: "$cashbackAmount", 
        then: 1, 
        else: 0 
       } 
      }, 
      unitPricePresent: { 
       $cond: { 
        if: "$unitPrice", 
        then: 1, 
        else: 0 
       } 
      } 
     } 
    }, 
    { 
     $group: { 
      "_id": "$siteid", 
      amountSum: { "$sum": "$amount" }, 
      amountCount: { "$sum": "$amountPresent" }, 
      totalAmountSum: { "$sum": "$totalAmount" }, 
      totalAmountCount: { "$sum": "$totalAmountPresent" }, 
      cashbackAmountSum: { "$sum": "$cashbackAmount" }, 
      cashbackAmountCount: { "$sum": "$cashbackAmountPresent" }, 
      unitPriceSum: { "$sum": "$unitPrice" }, 
      unitPriceCount: { "$sum": "$unitPricePresent" } 
     } 
    } 
]) 
+0

是的,謝謝你的工作太 – user3782299

+0

然後請投票回答哪些幫助你(不僅是我的):) – DAXaholic

+0

當我獲得聲望時會做 – user3782299

1

如果您事先知道金額字段,那麼您可以在單個聚合操作中動態創建管道。

看看下面的演示:

var amountFields = ["amount", "totalAmount", "cashbackAmount", "unitPrice"], 
    groupOperator = { "$group": { "_id": "$siteid" } }; 

amountFields.forEach(function (field){ 
    groupOperator["$group"][field+"Total"] = { "$sum": "$"+field }; 
    groupOperator["$group"][field+"Count"] = { 
     "$sum": {    
      "$cond": [ { "$gt": [ "$"+field, null ] }, 1, 0 ] 
     } 
    }; 
}); 

db.test.aggregate([groupOperator]); 

填充測試文件

db.test.insert([ 
    { 
     "siteid": "123456", 
     "amount": 1.32 
    }, 
    { 
     "siteid": "123456", 
     "cashbackAmount": 8.32 
    }, 
    { 
     "siteid": "123456", 
     "cashbackAmount": 9.74 
    }, 
    { 
     "siteid": "123456", 
     "unitPrice": 0.19 
    }, 
    { 
     "siteid": "123456", 
     "amount": 27.8, 
     "totalAmount": 15.22, 
     "unitPrice": 5.10, 
     "cashbackAmount": 43.62  
    }, 
    { 
     "siteid": "123456", 
     "unitPrice": 5.07 
    }, 
    { 
     "siteid": "123456", 
     "amount": 12.98, 
     "totalAmount": 32.82 
    }, 
    { 
     "siteid": "123456", 
     "amount": 6.65, 
     "unitPrice": 5.10 
    } 
]) 

樣品聚集輸出

{ 
    "_id" : "123456", 
    "amountTotal" : 48.75, 
    "amountCount" : 4, 
    "totalAmountTotal" : 48.04, 
    "totalAmountCount" : 2, 
    "cashbackAmountTotal" : 61.68, 
    "cashbackAmountCount" : 3, 
    "unitPriceTotal" : 15.46, 
    "unitPriceCount" : 4 
} 
+0

謝謝「$ cond」:[{「$ gt」:[「$」+ field,null]},1,0]是要走的路 – user3782299