I have a Mongodb schema that looks roughly like:
[
{
\"name\" : \"name1\",
\"instances\" : [
{
\"value\" : 1,
\"date\" : ISODate
You can use below aggregation:
db.col.aggregate([
{
$project: {
instances: {
$map: {
input: { $range: [ 0, { $size: "$instances" }, N ] },
as: "index",
in: { $arrayElemAt: [ "$instances", "$$index" ] }
}
}
}
}
])
$range generates a list of indexes. Third parameter represents non-zero step. For N = 2
it will be [0,2,4,6...]
, for N = 3
it will return [0,3,6,9...]
and so on. Then you can use $map to get correspinding items from instances
array.