$slice
The $slice aggregation operator enables you to return a subset of an array by either traversing the array from the beginning or the end of the array. This is used to display a limited number of items from an array field, such as the top or bottom N items.
Parameters
-
array: The array field to be sliced. -
n: An integer that specifies the number of elements to return. A positive value starts from the beginning of the array, while a negative value starts from the end of the array.
Example (MongoDB Shell)
The following example demonstrates how to use $slice to return the first two favorite sweets for each chef.
Create sample documents
db.sweets.insertMany([ { "_id" : 1, "name" : "Alvin", "favorites": [ "chocolate", "cake", "toffee", "beignets" ] }, { "_id" : 2, "name" : "Tom", "favorites": [ "donuts", "pudding", "pie" ] }, { "_id" : 3, "name" : "Jessica", "favorites": [ "fudge", "smores", "pudding", "cupcakes" ] }, { "_id" : 4, "name" : "Rachel", "favorites": [ "ice cream" ] } ]);
Query example
db.sweets.aggregate([ { $project: { _id: 0, name: 1, topTwoFavorites: { $slice: [ "$favorites", 2 ] } } } ]);
Output
[
{ name: 'Alvin', topTwoFavorites: [ 'chocolate', 'cake' ] },
{ name: 'Tom', topTwoFavorites: [ 'donuts', 'pudding' ] },
{ name: 'Jessica', topTwoFavorites: [ 'fudge', 'smores' ] },
{ name: 'Rachel', topTwoFavorites: [ 'ice cream' ] }
]
In this example, the $slice operator is used to extract the first two elements from the favorites array for each document.
Code examples
To view a code example for using the $slice command, choose the tab for the language that you want to use: