$slice
The $slice projection operator limits the number of array elements returned in a query result. It allows you to retrieve a specific number of elements from the start or end of an array field without loading the entire array.
Parameters
-
field: The array field to project. -
count: Number of elements to return. Positive values return elements from the start, negative values from the end.
Example (MongoDB Shell)
The following example demonstrates how to use the $slice projection operator to return only the first two items from an array field.
Create sample documents
db.inventory.insertMany([ { _id: 1, item: "notebook", tags: ["office", "school", "supplies", "writing"] }, { _id: 2, item: "pen", tags: ["office", "writing"] }, { _id: 3, item: "folder", tags: ["office", "supplies", "storage", "organization"] } ]);
Query example
db.inventory.find( {}, { item: 1, tags: { $slice: 2 } } )
Output
{ "_id" : 1, "item" : "notebook", "tags" : [ "office", "school" ] }
{ "_id" : 2, "item" : "pen", "tags" : [ "office", "writing" ] }
{ "_id" : 3, "item" : "folder", "tags" : [ "office", "supplies" ] }
Code examples
To view a code example for using the $slice projection operator, choose the tab for the language that you want to use: