In MongoDB all documents have a date field, it is a timestamp.
There is a lot of data, and I want to get only some part of it, for every interval:
e.g. 400ms
1402093316030<----
1402093316123
1402093316223
1402093316400<----
1402093316520
1402093316630
1402093316824<----
Is it possible to get every other, or every third result?
Or better first document every 400 ms?
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.