2016-01-12 136 views
2

我想在Mongodb中執行查詢。我想要執行的查詢是根據日期(過去7天)查找集合中的所有訂單,然後將每個訂單的嵌套對象與價格相加。我有以下代碼至今:基於過去日期的Mongodb聚合

收集/數據

{ 
    "_id" : "g32fYpydfSFDbFkoi", 
    "orderNumber" : 1234, 
    "createdAt" : ISODate("2016-01-12T13:50:17.559Z"), 
    "productsInOrder" : [ 
     { 
      "category" : "ambient", 
      "item" : 23982, 
      "desc" : "Ergonomic Cotton Sausages", 
      "quantity" : "456", 
      "price" : "0.54", 
      "lineprice" : "246.24", 
      "_id" : "BdD4QnM7sYTwBpLds" 
     }, 
     { 
      "category" : "ambient", 
      "item" : 15336, 
      "desc" : "Rustic Wooden Chicken", 
      "quantity" : "2", 
      "price" : "1.87", 
      "lineprice" : "3.74", 
      "_id" : "PvtSxi2MfYrZNTD6f" 
     }, 
     { 
      "category" : "chilled", 
      "item" : 57584, 
      "desc" : "Unbranded Soft Chicken", 
      "quantity" : "3", 
      "price" : "4.69", 
      "lineprice" : "14.07", 
      "_id" : "ppkECqmhPvg7pQcgB" 
     }, 
     { 
      "category" : "ambient", 
      "item" : 71168, 
      "desc" : "Rustic Rubber Computer", 
      "quantity" : "5", 
      "price" : "3.04", 
      "lineprice" : "15.20", 
      "_id" : "bZtr5dkvsG92YtLoe" 
     }, 
     { 
      "category" : "frozen", 
      "item" : 87431, 
      "desc" : "Unbranded Granite Sausages", 
      "quantity" : "5678", 
      "price" : "1.98", 
      "lineprice" : "11242.44", 
      "_id" : "ZKur3rHhtCLsWiENr" 
     }, 
     { 
      "category" : "frozen", 
      "item" : 75007, 
      "desc" : "Practical Frozen Towels", 
      "quantity" : "678", 
      "price" : "1.19", 
      "lineprice" : "806.82", 
      "_id" : "g78zvzoE8wJkciD9C" 
     }, 
     { 
      "category" : "frozen", 
      "item" : 84721, 
      "desc" : "Fantastic Metal Hat", 
      "quantity" : "34", 
      "price" : "1.83", 
      "lineprice" : "62.22", 
      "_id" : "4aqxBWhXy5cabbbiM" 
     }, 
     { 
      "category" : "frozen", 
      "item" : 72240, 
      "desc" : "Fantastic Granite Towels", 
      "quantity" : "1", 
      "price" : "2.94", 
      "lineprice" : "2.94", 
      "_id" : "MQD2LNv36mE3BWvZJ" 
     }, 
     { 
      "category" : "chilled", 
      "item" : 89448, 
      "desc" : "Intelligent Concrete Towels", 
      "quantity" : "6678", 
      "price" : "0.42", 
      "lineprice" : "2804.76", 
      "_id" : "AjRrxFT4mfpxuciC4" 
     }, 
     { 
      "category" : "chilled", 
      "item" : 57584, 
      "desc" : "Unbranded Soft Chicken", 
      "quantity" : "1111", 
      "price" : "4.69", 
      "lineprice" : "5210.59", 
      "_id" : "4yBspve6mBNNzqDnZ" 
     } 
    ] 
    } 

查詢

Orders.aggregate([ 
    { $match: { 'createdAt': { $gt: pastDate }}}, 
    { $unwind: '$productsInOrder' }, 
    { 
     $group: { 
     _id: null, 
     price: { 
      $sum: '$productsInOrder.price' 
     } 
     } 
    } 
]); 

我最終想要的是輸出每天的總價格在過去的7天。任何人都可以幫助我指出正確的方向嗎?提前謝謝了。

+0

你知道你的'price'字段是一個字符串嗎? – chridam

+1

@chridam不,我沒有!感謝您指出這一點 – mtwallet

+0

@chridam現在我已經糾正了這個問題,我可以得到所有價格的總和,所以感謝指針。有沒有辦法爲過去7天中的每一天單獨輸出一個總數,還是必須針對每個條件運行多個查詢? – mtwallet

回答

1

首先,$sum操作者會忽略非數值與productsInOrder.price子文檔字段是字符串類型的,所以如果你將它轉換爲數字領域那將是最好的。

已經這樣做了,輸出每天的總價格爲過去7天,通過鍵更改組使用$dayOfMonth操作這組的有7天的範圍內每天的文檔,如下面的

Orders.aggregate([ 
    { "$match": { "createdAt": { "$gt": pastDate } } }, 
    { "$unwind": "$productsInOrder" }, 
    { 
     "$group": { 
      "_id": { 
       "day": { "$dayOfMonth": "$createdAt" } 
      }, 
      "price": { "$sum": "$productsInOrder.price" } 
     } 
    }  
]);