I have a mongo collection with 7 million documents, besides a couple of other fields each document has a 'createdAt' Date object. I have an index 'createdAt:1' on the field and it's hosted at a dedicated mongo service.
When I try to group by day the query gets real slow. Here is my aggregation query:
{
"$match": {
"createdAt": {
$gte:new Date(1472189560111)
}
}
},
{
"$project": {
"date":
{
"$dateToString": {
"format": "%Y-%m-%d",
"date": "$createdAt"
}
},
"count": 1
}
},
{
"$group": {
"_id": "$date",
"count": {
"$sum": 1
}
}
},
{
"$sort": {
"_id": 1
}
},
{
"$project": {
"date": "$_id",
"count": 1,
"_id": 0
}
}
What's a good strategy to improve the performance? Is there a problem in my aggregation pipeline? Do I need a field that contains the day date object with a fixed time like 00:00 and group on that? It seems such a basic operation that I believe there has to be a mongodb native way of doing that.