You can do this with an aggregation structure and a little math. Say you have a "timestamp" field and additional 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" } }} ])
Thus, the mathematical date there โgroupsโ according to the values โโof the โtimestampโ field with an interval of 400 ms. The rest of the data is identified using the $first operator, which selects the "last" value from the field as shown at these grouping boundaries.
If you otherwise ran the "last" element at these boundaries, you switch to using the $last statement.
The end result is the last document that occurred every 400 millisecond interval.
See the aggregate command and Aggregate Structure Operators for more reference.
source share