Hello everyone,
I’m learning MongoDB and I’m practicing with a traffic database, and I’d like to know how to get for each radar the time of the day (“Record.time”) when the most speeding tickets are recorded (the most repeated hour for each radar, something like radar X (with its attributes) and its “happy hour” )
This is how a document looks like in the database:
{
"_id" : "20110418123336260",
"radar" : {
"mileage" : "15",
"direction" : "ascending",
"speed limit" : "180"
},
"Record" : {
"date" : "18/04/2007",
"time" : "12:33:36.260", //Hour<24 and .260 are ms
"speed" : "193"
}
The closest I’ve ever come is to find out the latest hour that each radar has fined with this query:
db.traffic.aggregate(
[
{
$group:
{
_id: "$radar",
poppularHour: { $max: "$Record.time" }
}
}
]
)
Also I found all the occurrences of all the hours with:
db.traffic.aggregate( [ { $unwind: "$radar" },{ $sortByCount: "$Record.time" } ] )
or the most repeated hour with it’s occurences with:
db.trafico.aggregate([
{ "$unwind": "$radar" },
{
"$group": {
"_id": "$Record.time",
"count": { "$sum": 1 }
}
},
{ "$sort": { "count": -1 } },
{ "$limit": 1 }
])
but as I said, I’m looking for each radar his most repeated hour, and even if I try everything I can’t get it.
Could please someone help me?
Thank you in advance!!
PD: When I say the most repeated hour I mean the same string Record.time (“HH:MM:SS:FFF”)