I have a collection called Document in MongoDB. Documents in this collection have a field called CreationDate stored in ISO date type. My task is to count the number of docu
You can do this with $concat but first you need to convert to a string via $substr, also handling the double digit case:
db.Document.aggregate([
{ "$group": {
"_id":{
"$concat": [
{ "$substr": [ { "$year": "$CreationDate" }, 0, 4 ] },
"-",
{ "$cond": [
{ "$gt": [ { "$month": "$CreationDate" }, 9 ] },
{ "$substr": [ { "$month": "$CreationDate" }, 0, 2 ] },
{ "$concat": [
"0",
{ "$substr": [ { "$month": "$CreationDate" }, 0, 1 ] },
]},
]},
"-",
{ "$cond": [
{ "$gt": [ { "$dayOfMonth": "$CreationDate" }, 9 ] },
{ "$substr": [ { "$dayOfMonth": "$CreationDate" }, 0, 2 ] },
{ "$concat": [
"0",
{ "$substr": [ { "$dayOfMonth": "$CreationDate" }, 0, 1 ] },
]}
]}
]
},
{ "cnt": { "$sum": 1 } }
}}
{ "$sort":{ "cnt" :-1 }}
]);
Possibly better is to just use date math instead, this returns an epoch timestamp value, but it is easy to work into a date object in post processing:
db.Document.aggregate([
{ "$group": {
"_id": {
"$subtract": [
{ "$subtract": [ "$CreationDate", new Date("1970-01-01") ] },
{ "$mod": [
{ "$subtract": [ "$CreationDate", new Date("1970-01-01") ] },
1000 * 60 * 60 * 24
]}
]
},
"cnt": { "$sum": 1 }
}},
{ "$sort": { "cnt": -1 } }
])