问题
I'm storing time series data in a mongoDB collection with one data point every 15min. But sometimes, due to bad conditions, some data points get lost. I have a dataset as follows:
{"device_id": "ABC","temp": 12,"timestamp": 2020-01-04T17:48:09.000+00:00}
{"device_id": "ABC","temp": 10,"timestamp": 2020-01-04T18:03:09.000+00:00}
{"device_id": "ABC","temp": 14,"timestamp": 2020-01-04T18:18:09.000+00:00}
missing frame
missing frame
{"device_id": "ABC","temp": 13,"timestamp": 2020-01-04T19:03:09.000+00:00}
{"device_id": "ABC","temp": 15,"timestamp": 2020-01-04T19:18:09.000+00:00}
missing frame
{"device_id": "ABC","temp": 10,"timestamp": 2020-01-04T19:48:09.000+00:00}
{"device_id": "ABC","temp": 11,"timestamp": 2020-01-04T20:03:09.000+00:00}
...
I can't figure out how I can query this collection in order to have a continuous list of value every 15min in order to plot it and displaying lost messages (by changing the background color of the graph in case of lost messages). I would like to have a result aligned on every 15min (which would sum the values between t and t+15min) like this:
{"timestamp": 2020-01-04T17:45:00.000+00:00, "temp": 12, missing: false}
{"timestamp": 2020-01-04T18:00:00.000+00:00, "temp": 10, missing: false}
{"timestamp": 2020-01-04T18:15:00.000+00:00, "temp": 14, missing: false}
{"timestamp": 2020-01-04T18:30:00.000+00:00, "temp": 0, missing: true}
{"timestamp": 2020-01-04T18:45:00.000+00:00, "temp": 0, missing: true}
{"timestamp": 2020-01-04T19:00:00.000+00:00, "temp": 13, missing: false}
{"timestamp": 2020-01-04T19:15:00.000+00:00, "temp": 15, missing: false}
{"timestamp": 2020-01-04T19:30:00.000+00:00, "temp": 0, missing: true}
{"timestamp": 2020-01-04T19:45:00.000+00:00, "temp": 10, missing: false}
{"timestamp": 2020-01-04T20:00:00.000+00:00, "temp": 11, missing: false}
Any ideas? Thanks in advance for your help!
回答1:
Here is aggregation with the approach I had mentioned in my first comment:
db.collection.aggregate( [
{
$sort: { timestamp: 1 }
},
{
$group: {
_id: null,
docs: { $push: { timestamp: "$timestamp", device_id: "$device_id", temp: "$temp", missing: false } },
device_id: { $first: "$device_id" },
start: { $first: { $toInt: { $divide: [ { "$toLong": "$timestamp" }, 1000 ] } } },
end: { $last: { $toInt: { $divide: [ { "$toLong": "$timestamp" }, 1000 ] } } }
}
},
{
$addFields: {
docs: {
$map: {
input: { $range: [ { $toInt: "$start" }, { $add: [ { $toInt: "$end" }, 900 ] }, 900 ] },
as: "ts",
in: {
ts_exists: { $arrayElemAt: [
{ $filter: {
input: "$docs", as: "d",
cond: { $eq: [ { $toInt: { $divide: [ { "$toLong": "$$d.timestamp" }, 1000 ] } },
"$$ts"
] }
}},
0 ] },
ts: "$$ts"
}
}
}
}
},
{
$unwind: "$docs"
},
{
$addFields: {
docs: {
$ifNull: [ "$docs.ts_exists", { timestamp: { $toDate: { $multiply: [ "$docs.ts", 1000 ] } },
temp: 0, device_id: "$device_id", missing: true
}
]
}
}
},
{
$replaceRoot: { newRoot: "$docs" }
}
] ).pretty()
Using the following input documents:
{"device_id": "ABC","temp": 12,"timestamp": ISODate("2020-01-04T17:45:00.000+00:00") },
{"device_id": "ABC","temp": 10,"timestamp": ISODate("2020-01-04T18:00:00.000+00:00") },
{"device_id": "ABC","temp": 4,"timestamp": ISODate("2020-01-04T18:30:00.000+00:00") },
{"device_id": "ABC","temp": 23,"timestamp": ISODate("2020-01-04T18:45:00.000+00:00") }
The result:
{
"timestamp" : ISODate("2020-01-04T17:45:00Z"),
"device_id" : "ABC",
"temp" : 12,
"missing" : false
}
{
"timestamp" : ISODate("2020-01-04T18:00:00Z"),
"device_id" : "ABC",
"temp" : 10,
"missing" : false
}
{
"timestamp" : ISODate("2020-01-04T18:15:00Z"),
"temp" : 0,
"device_id" : "ABC",
"missing" : true
}
{
"timestamp" : ISODate("2020-01-04T18:30:00Z"),
"device_id" : "ABC",
"temp" : 4,
"missing" : false
}
{
"timestamp" : ISODate("2020-01-04T18:45:00Z"),
"device_id" : "ABC",
"temp" : 23,
"missing" : false
}
来源:https://stackoverflow.com/questions/61217428/mongodb-how-to-query-a-time-series-with-incomplete-data