You could start by projecting a new date field that you can then group by the interval.
Suppose you have the following test documents:
db.collection.insert([
{
groupID: '1234',
name: 'dataPointName',
timestamp: 1432765200000,
value: 1234
},
{
groupID: '1234',
name: 'dataPointName',
timestamp: 1432765300000,
value: 1234
},
{
groupID: '1234',
name: 'dataPointName',
timestamp: 1432766100000,
value: 1234
}
])
You can then implement the following aggregation:
db.collection.aggregate([
{
"$project": {
"date": { "$add": [new Date(0), "$timestamp"] },
"timestamp": 1,
"value": 1
}
},
{
"$group": {
"_id": {
"year": { "$year": "$date" },
"dayOfYear": { "$dayOfYear": "$date" },
"interval": {
"$subtract": [
{ "$minute": "$date" },
{ "$mod": [{ "$minute": "$date"}, 10 ] }
]
}
},
"grouped_data": { "$push": {"timestamp": "$timestamp", "value": "$value" } }
}
},
{
"$project":{
"_id": 0,
"grouped_data": 1
}
}
])
Output:
/* 0 */
{
"result" : [
{
"grouped_data" : [
{
"timestamp" : 1432766100000,
"value" : 1234
}
]
},
{
"grouped_data" : [
{
"timestamp" : 1432765200000,
"value" : 1234
},
{
"timestamp" : 1432765300000,
"value" : 1234
}
]
}
],
"ok" : 1
}
-- EDIT --
To format the data as array like [timestamp,value] rather than a key/value array, you could use the forEach() method of the aggregate cursor as follows:
var result = [];
db.collection.aggregate(pipeline).forEach(function (doc){
data = [];
doc.grouped_data.forEach(function (obj){
data.push(obj.timestamp);
data.push(obj.value);
});
result.push(data);
})
printjson(result);
Output
[
[
1432766100000,
1234
],
[
1432765200000,
1234,
1432765300000,
1234
]
]