2016-01-12 97 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" } 
     } 
    }  
]);