Count total days filter mongodb
Solution 1:
Maybe something like this:
db.collection.aggregate([
{
$project: {
type: 1,
created_at: {
$dateFromString: {
dateString: "$created_at"
}
}
}
},
{
"$match": {
"created_at": {
"$gte": ISODate("2022-01-01"),
"$lte": ISODate("2022-01-22")
}
}
},
{
$group: {
_id: "total",
totalq: {
$sum: 1
},
maxd: {
$max: "$created_at"
},
mind: {
$min: "$created_at"
}
}
},
{
$project: {
_id: 0,
total_result_query: {
$toInt: "$totalq"
},
total_days_day_filter: {
$toInt: {
$dateDiff: {
startDate: "$mind",
endDate: "$maxd",
unit: "day"
}
}
}
}
}
])
playground
playground ( + search filter total days )