Count of records by Date MongoDB
if you want to count by lastModified
date and time then can use just like:
db.getCollection('collectionName').aggregate({$group:{_id: "$lastModified", count:{$sum:1}}})
and if you want to count by lastModified
date only then can use just like:
db.getCollection('collectionName').aggregate(
[
{
$group:
{
_id:
{
day: { $dayOfMonth: "$lastModified" },
month: { $month: "$lastModified" },
year: { $year: "$lastModified" }
},
count: { $sum:1 },
date: { $first: "$lastModified" }
}
},
{
$project:
{
date:
{
$dateToString: { format: "%Y-%m-%d", date: "$date" }
},
count: 1,
_id: 0
}
}
])
This is simpler:
db.getCollection('collectionName').aggregate(
[
{
$group:
{
_id: {
$dateToString: {
"date": "$lastModified",
"format": "%Y-%m-%d"
}
},
count: { $sum:1 }
}
}
])