You can do this with the aggregation framework and a little date math. Let's say you have a "timestamp" field and addtional fields "a", "b" and "c":
db.collection.aggregate([
{ "$group": {
"_id": {
"$subtract": [
"$timestamp",
{ "$mod": [ "$timestamp", 400 ] }
]
},
"timestamp": { "$first": "$timestamp" },
"a": { "$first": "$a" },
"b": { "$first": "$b" },
"c": { "$first": "$c" }
}}
])
So the date math there "groups" on the values of the "timestamp" field at 400ms intervals. The rest of the data is identified with the $first
operator, which picks the "last" value from the field as found on those grouping boundaries.
If you otherwise wan the "last" item on those boundaries then you switch to use the $last
operator instead.
The end result is the last document that occurred every 400 millisecond interval.
See the aggregate command and the Aggregation Framework operators for additional reference.