Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

$slice projection unexpected behavior when using negative skip #12474

Closed
2 tasks done
phoenixstudiodz opened this issue Sep 26, 2022 · 1 comment · Fixed by #12776
Closed
2 tasks done

$slice projection unexpected behavior when using negative skip #12474

phoenixstudiodz opened this issue Sep 26, 2022 · 1 comment · Fixed by #12776
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary

Comments

@phoenixstudiodz
Copy link

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

6.3.4

Node.js version

16.14.2

MongoDB server version

5.0.9

Description

The docs in $slice: [ <number to skip>, <number to return> ] state that If the number of skiped elements is greater than the number of array elements, the query returns an empty array.
I expected a similar behavior when the skiped number is negative but it wasn't the case (when specifying the number of returned elements).
It seems that that when n (number of skip) is negative and greater than the array size it become 0 like if(n > array.length) return 0 where 0 is the starting referance.

Steps to Reproduce

Given {array : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] }

  • myCollection.findOne({}, { $slice: [-1, 5] }); returns: [ 9, 10]
  • myCollection.findOne({}, { $slice: [-4, 5] }); returns: [6, 7, 8, 9, 10]
  • myCollection.findOne({}, { $slice: [-9, 5] }); returns: [1, 2, 3, 4, 5]
  • myCollection.findOne({}, { $slice: [-10, 5] }); returns: [1, 2, 3, 4, 5]
  • myCollection.findOne({}, { $slice: [-100, 5] }); returns: [1, 2, 3, 4, 5]

Expected Behavior

I expected that myCollection.findOne({}, { $slice: [-100, 5] }); to return an empty array same as myCollection.findOne({}, { $slice: [-10, 5] }); to return: [1, 2, 3, 4], meaning it should work same way netagive or possitive. the expected behavior should be in my opinion the same when :

  • using negative skip with number of return
  • or when using array.reverse() then a skip with a positive number.
    Meaning both return empty array if the skip is too big
@IslandRhythms IslandRhythms added the docs This issue is due to a mistake or omission in the mongoosejs.com documentation label Sep 29, 2022
@vkarpov15 vkarpov15 added this to the 6.6.6 milestone Oct 7, 2022
@vkarpov15 vkarpov15 modified the milestones: 6.6.6, 6.6.7, 6.6.8, 6.7.1, 6.7.2 Oct 19, 2022
@vkarpov15 vkarpov15 modified the milestones: 6.7.3, 6.7.4, 6.7.7 Nov 22, 2022
lpizzinidev added a commit to lpizzinidev/mongoose that referenced this issue Dec 7, 2022
@vkarpov15
Copy link
Collaborator

#12776 explains that this is expected behavior. From the following docs: https://www.mongodb.com/docs/manual/reference/operator/projection/slice/

image

@vkarpov15 vkarpov15 removed this from the 6.8.2 milestone Dec 16, 2022
@vkarpov15 vkarpov15 added the help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary label Dec 16, 2022
vkarpov15 added a commit that referenced this issue Dec 16, 2022
docs(query): updated explanation for slice
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants