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
To convert date format to "xxxx-xx-xx"
db.getCollection('analytics').aggregate([
{$project: {
day: {$dayOfMonth: "$createdAt"},
month: {$month: "$createdAt"},
year: {$year: "$createdAt"},
}
},
{$project: {
day: {$concat:["0", {$substr:["$day",0, 2]}]},
month: {$concat:["0", {$substr:["$month",0, 2]}]},
year: {$substr:["$year",0, 4]},
}
},
{$project: {date: {$concat: [{$substr:["$year",0, 4]},"-", {$substr:["$month",0, 2]},"-", {$substr:["$day",0, 2]}]}}},
]);