Hello, I am new to this community.
I have been trying out to use $slice in my aggregation pipeline. Somehow it is not working as expected, the first index is not getting recognized.
The “dataArr” does resolves to an array (it is an array of Objects), and i am unsure why the $slice operator is not working.
Hi @turivishal , thanks for your response!
I read the documentation, and expect data: {$slice: [“$dataArr”, 5, 10]} to return an array of 5 elements, but yet at stage 3 of my aggregation, the “data” variable is an array of 10 elements instead of 5.
I did try in stage 3 to use $slice on a normal array, testSlice: {$slice: [[1,2,3,4,5], 2, 3]}, and “testSlice” variable does return an array of 3 elements.
So im unsure why $slice does not work when im using in on the array i obtained from stage 2 of my aggregation pipeline and return me 10 elements instead of 5.
I did look at the two, based on my understanding, one is used for aggregation() and the other for find() right? And in any case, i should expect an array of 5 elements instead of 10 when i apply $slice on “$dataArr” right?
Can you provide some explanation as to why line 6 of stage 3 of the aggregation pipeline produces an array of 10 elements when i use $slice: ["$dataArr, 5, 10] and should only give 5 elements.
Of course, it will give 10 elements if there are 10 elements from the 5th index because you have passed 10 in the third argument.
Can you check the total elements in dataArr using dataArrSize: { $size: "$dataArr"} in $project stage? As per your screenshot, the $group stage shows just 3 elements.
You can reproduce the issue in this playground also and share the link.
Thanks for your reply and clarification! I did not read the documents clearly and assumed the slicing works in a manner similar python
Thanks for your patience and help! Greatly appreciate it