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

docs(query): updated explanation for slice #12776

Merged
merged 1 commit into from
Dec 16, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,17 +370,33 @@ Query.prototype.clone = function clone() {
*
* #### Example:
*
* query.slice('comments', 5);
* query.slice('comments', -5);
* query.slice('comments', [10, 5]);
* query.where('comments').slice(5);
* query.where('comments').slice([-10, 5]);
* query.slice('comments', 5); // Returns the first 5 comments
* query.slice('comments', -5); // Returns the last 5 comments
* query.slice('comments', [10, 5]); // Returns the first 5 comments after the 10-th
* query.where('comments').slice(5); // Returns the first 5 comments
* query.where('comments').slice([-10, 5]); // Returns the first 5 comments after the 10-th to last
*
* **Note:** If the absolute value of the number of elements to be sliced is greater than the number of elements in the array, all array elements will be returned.
*
* // Given `arr`: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
* query.slice('arr', 20); // Returns [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
* query.slice('arr', -20); // Returns [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
*
* **Note:** If the number of elements to skip is positive and greater than the number of elements in the array, an empty array will be returned.
*
* // Given `arr`: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
* query.slice('arr', [20, 5]); // Returns []
*
* **Note:** If the number of elements to skip is negative and its absolute value is greater than the number of elements in the array, the starting position is the start of the array.
*
* // Given `arr`: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
* query.slice('arr', [-20, 5]); // Returns [1, 2, 3, 4, 5]
*
* @method slice
* @memberOf Query
* @instance
* @param {String} [path]
* @param {Number} val number/range of elements to slice
* @param {Number|Array} val number of elements to slice or array with number of elements to skip and number of elements to slice
* @return {Query} this
* @see mongodb https://www.mongodb.org/display/DOCS/Retrieving+a+Subset+of+Fields#RetrievingaSubsetofFields-RetrievingaSubrangeofArrayElements
* @see $slice https://docs.mongodb.org/manual/reference/projection/slice/#prj._S_slice
Expand Down