Reputation: 3
I have mongodb Database and I have this document structure :
{
"_id": ObjectId("2145214545ffff"),
"arr":[a , b , c , d , e , f , g , h , i, j]
}
I want to execute aggregation pipeline , which gives me this result:
{
"_id": ObjectId("2145214545ffff"),
"arr":[a , d , g , j ]
}
So what i need is kind of filtering on the array's items , which give me the 1st , 4th , 7th and so on items. Thx in advanced
Upvotes: 0
Views: 211
Reputation: 5048
You need to use $map
and $range
operators.
The following query will be helpful:
db.collection.aggregate([
{
$project: {
arr: {
$map: {
input: {
$range: [
0,
{
$size: "$arr"
},
3
]
},
as: "a",
in: {
$arrayElemAt: [
"$arr",
"$$a"
]
}
}
}
}
}
])
Upvotes: 1