In aggregation project array element from index x to y where x is defined inside collection and y is an array element corresponding to the index of the array element which is matched. Please see below example then it will become quite easier to understand what i am trying to say.
coupons collection
{"coupon_id": "coupon01", "codes": ["FLAT30", "FLAT50", "FLAT70", "FLAT90"], "curr_index": 0}
For example see below example code here i am trying to get coupon codes starting from curr_index to (curr_index + n) where n is the number in coupon_ctrs corresponding to the index of coupons_ids example- for id "584559bd1f65d363bd5d25fd" n is 1, for id "58455a5c1f65d363bd5d2600" n is 2 and for the id "584878eb0005202c64b0cc5d" n is 3.
coupons_ctrs = [1, 2, 3];
coupons_ids = ["584559bd1f65d363bd5d25fd", "58455a5c1f65d363bd5d2600", "584878eb0005202c64b0cc5d"];
int n = 2;
couponmodel.aggregate(
{ $match : { '_id': { $in : coupons_ids }} },
{ $project: {_id:0, codes : /* How to use slice here so that codes array will be returned between cur_index and (curr_index + coupons_ctr corresponding to the index coupon id is found, example- for _id "584559bd1f65d363bd5d25fd" it should be 1 and so on) */} },
function(err, docs) {
if (err) {
} else {
}
});
UPDATE
As suggested by Styvane i could use $zip and it would work perfect but as i am using mongoDB 3.2.11 so i can't use it, so what can be the solution for using functionality of $zip in mongodb 3.2.11 ??
Can anyone please tell me how can i include this coupon_ctrs array and use it inside aggregation pipeline.