我正在尝试计算平均响应时间(excluding weekends: Saturday, Sunday and particular dates for holidays and non working hours(10:00AM to 06:00PM)
),但无法弄清楚该如何做,以下是文档的架构:
{
"_id" : ObjectId("5dc91170b15e1f7ae332b9fe"),
"actions": {
"created" : ISODate("2019-11-07T07:44:48.799Z"),
"accepted" : ISODate("2019-11-12T07:44:48.799Z")
}
},
{
"_id" : ObjectId("5dc929bffc638655004a691e"),
"actions": {
"created" : ISODate("2019-11-06T09:28:31.039Z"),
"accepted" : ISODate("2019-11-12T04:48:09.054Z")
}
},
{
"_id" : ObjectId("5dca3b2a6e78f16598ae462b"),
"actions": {
"created" : ISODate("2019-11-12T04:55:06.520Z"),
"accepted" : ISODate("2019-11-12T04:55:06.520Z")
}
},
{
"_id" : ObjectId("5dca3bc4dd8d4fe1b0b1b15e"),
"actions": {
"created" : ISODate("2019-11-12T04:57:40.396Z"),
"accepted" : ISODate("2019-11-12T04:57:40.396Z")
}
}
我正在使用的查询不排除非工作日和工作时间:
db.claims.aggregate([
{
$group: {
_id: null,
total_time_count: {
$sum: {
$cond: [
{ $ifNull: ['$actions.accepted', 0] },
1,
0
]
}
},
total_time: {
$sum: {
$cond: [
{ $ifNull: ['$actions.accepted', 0] },
{ $subtract: ['$actions.accepted', '$actions.created'] },
0
]
}
}
}
},
{
$project: {
average_response_time: {
$cond: [
{ $gt: ['$total_time_count', 0] },
{ $divide: ['$total_time', '$total_time_count'] },
0
]
}
}
}
]);
[如果有人在类似情况下工作,请作答。
感谢您的任何帮助,谢谢。
对不起,我没有时间为您解决此问题。看看这个apihttps://docs.mongodb.com/manual/reference/operator/aggregation/dayOfWeek/