I am using mLab which doesn't seem to want to let me use ISODate("2016-06-16T00:00:00.201Z")
so I end up having to have all my dates in mLab as a string line "transaction_date": "2016-06-16T00:00:00.201Z"
.
Having a stringified date seems to work fine for my data ranges:
$match {
"transaction_date" : {$gte: "2016-04-01"},
"transaction_date" : {$lt: "2016-08-01"},
}
However it doesn't like using the date when I attempt to do a final group to sum my results by month number.
$group {
"revenue_month" : {$month : "transaction_date"},
"net_revenue" : {$sum: "item_net_total_value"
}
Any ideas why I'm getting 'unknown group operator $month' message? Thanks
Here's the full script below:
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
]
);
Well from the docs about the $group
operator:
... The argument can be any expression as long as it resolves to a date ...
As you pass the operator a string it clearly does not resolve to a date and therefore does not work.
A workaround for this would be to take the proper substring of the date
$group: {
_id: { $substr : ["$transaction_date", 5, 2 ] },
"net_revenue" : { $sum: "item_net_total_value" },
}
BTW: I am not using mLab but I would be surprised if they don't allow dates. See this answer as intermediate 'solution' but I would suggest either creating a new question with the problem you have with mLab and dates or just ask someone of them to be sure your guessed restriction is true, because in the long term it is best to use the proper datatype.