|
Note that this can be done using existing expressions, so this request would be for simpler syntax.
For $format
db.data.aggregate( {$group:{_id:{$let:{
|
vars:{dt:{$dateToParts:{date:"$item.CreatedDate"}}},
|
in:{$concat:[
|
{$cond:[{$gt:["$$dt.month",9]},'','0']},
|
{$toString:"$$dt.month"},
|
"/",
|
{$toString:"$$dt.year"}
|
]}
|
}}}})
|
For $map there's several ways to do it, but if you already have an array of mappings in (as an example) this format:
months=[ { from: 1, to : "January" }, { from:2, to: "February" }, {from:3, to: "March"}, {from: 4, to: "April"}, ... ]
|
You can now do something similar to this:
db.data.aggregate({$group:{
|
_id:{$arrayElemAt:[
|
months.map(x=>x.to),
|
{$indexOfArray:[
|
months.map(x=>x.from),
|
{$month:"$item.CreatedDate"}
|
]}
|
]},
|
sum:{$sum:1}
|
}})
|
{ "_id" : "March", "sum" : 1 }
|
{ "_id" : "January", "sum" : 1 }
|
{ "_id" : "April", "sum" : 2 }
|
Note this uses new 3.7.4 $toString function, but previous versions supported the same thing via $substr expression (which when given a number coerce it to a string).
|