Can this be a correct mongodb document structure where keys are generated dynamically?
{
vehicle_id: '1234',
month: '01-17',
speed: {
1: { //day 1 -> 30
0: 50, //hour 0 -> 23
1: 55,
19: 90
},
2: {
0: 45,
1: 70,
2: 100
}
}
}
If I need to track a certain vehicle speed for whole month each day around the clock. Would this document structure be correct and sufficient? Bear in mind that the day and hour are generated dynamically.
If so, how can I run an aggregation to get the vehicle speed across certain days/hours interval?
EDIT:
I tried this aggregation:
{$project:{
vehicle_id: 1, month : 1,
days: {$objectToArray: "$speed" }
}},
{$unwind: '$days'},
{$match:{
month:'01-17',
'days.k': {
$eq: '30'
}
}},
and I get this result :
{ _id: 'db_id,
vehicle_id: '1234',
month: '01-17',
days:
{ k: '30',
v:
{ '0': 20,
'1': 30,
'2': 62,
'3': 31,
'4': 20,
'5': 23,
'6': 29, } } }
which is correct. When I try to change the match condition for the days to be:
'days.k': {$gt: '30'}
Then I should the results containing value greater than 30 which is mainly day 31. This happens but I also get the values from k: '1' -> k: '9'
. Is there something wrong with the operation I am doing?
Update:
I have managed to do some modifications to get the speed across a certain interval in a month: as follows:
{$match:{
month: '01-17',
'days.k': {
$in: dayValues
}
}}
But if I add more match conditions to get another month speed but different day values, nothing is returned.
{$match:{
month: '01-17',
'days.k': {
$in: dayValues1
}
}},
{$match:{
month: '02-17',
'days.k': {
$in: dayValues2
}
}}
Any idea how to make sure each condition gets applied and all results are returned?