$ group使用$ month不在字母化日期工作

时间:2016-07-31 02:06:08

标签: mongodb mongodb-aggregation

我正在使用mLab,它似乎不想让我使用ISODate("2016-06-16T00:00:00.201Z")所以我最终必须将所有日期都在mLab中作为字符串行"transaction_date": "2016-06-16T00:00:00.201Z"

有一个字符串化日期似乎适用于我的数据范围:

$match {
   "transaction_date" : {$gte: "2016-04-01"}, 
   "transaction_date" : {$lt: "2016-08-01"}, 
       }

然而,当我尝试按月份编号对结果进行求和时,它并不喜欢使用日期。

$group {
   "revenue_month" : {$month : "transaction_date"},
   "net_revenue" : {$sum: "item_net_total_value"
       }

任何想法,为什么我会得到未知的集团运营商$ month'信息?感谢

以下是完整的脚本:

db.datawarehouse.aggregate(

// Pipeline
[
// Stage 1 - Isolate correct document types
{
  $match: {
  "object_class" : "Goods & Services Transaction", 
  "object_type": "Transaction", 
  "object_category" : "Revenue",
  "object_origin_category" : "Bookkeeping"
  }
},

// Stage 2 - Restrict data set by date range
{
  $match: {
  "transaction_date" : {$gte: "2016-04-01"}, 
  "transaction_date" : {$lt: "2016-08-01"}, 
  }
},

// Stage 3 - For duplicate handling, sort by invoice number in descending creation date order
{
  $sort: {
  "transaction_reference":1, 
  "object_creation_date": -1
  }
},

// Stage 4 - For duplicate handing use $first to use latest version of all documents
{
  $group: {
  _id: "$transaction_reference", 
  "party_uuid" : {$first: "$party_uuid"},
  "transaction_date" : {$first: "$transaction_date"},
  "transaction_net_value" : {$first: "$transaction_net_value"},
  "object_category" : {$first: "$object_category"},
  "transaction_status" : {$first: "$transaction_status"},
  "object_origin_category" : {$first: "$object_origin_category"},
  "object_origin" : {$first: "$object_origin"},
  "customer" : {$first: "$party.customer.customer_name"},
  "goods_and_services" : {$first: "$goods_and_services"}
  }
},

// Stage 5 - Widen set from transaction level to line item level
{
  $unwind: "$goods_and_services"
},

// Stage 6 - Make the complete data set required per record
{
  $project: {
  {"_id" : 1,
    "party_uuid" : "$party_uuid",
    "transaction_date" : "$transaction_date",
    "transaction_net_value" : "$transaction_net_value",
    "object_category" : "$object_category",
    "object_origin_category" : "$object_origin_category",
    "object_origin" : "$object_origin",
    "item_quantity" : "$goods_and_services.item_quantity",
    "item_name" : "$goods_and_services.item_name",
    "item_category" : "$goods_and_services.item_category",
    "item_ net_total_sale_value" : "$goods_and_services.item_net_total_value",
  }
},

// Stage 7 - Restrict records by item category
{
  $match: {
  "item_category" : "Sales Revenue"
  }
}
// Stage 8 - Group by transaction_date and SUM item_net_total_value
]

);

1 个答案:

答案 0 :(得分:1)

关于$group运营商的docs

  

...只要解析为日期,参数就可以是任何表达式......

当您向运算符传递一个字符串时,它显然无法解析为日期,因此无效。

解决方法是采用日期的正确子字符串

$group: {
    _id: { $substr : ["$transaction_date", 5, 2 ] },
    "net_revenue" : { $sum: "item_net_total_value" },
}
顺便说一句:我没有使用mLab,但如果他们不允许约会,我会感到惊讶。将此答案视为中间“解决方案”,但我建议您使用mLab和日期问题创建一个新问题,或者只是让他们中的某个人确定您的猜测限制是正确的,因为从长远来看,最好是使用正确的数据类型。