Visit complete MongoDB roadmap
MongoDB Topic

$slice

$slice

The $slice projection operator is a MongoDB feature that allows you to limit the number of elements returned for an array field within the documents. This is particularly useful when you have large arrays in your documents, and you only need to work with a specific portion of them. By applying the $slice operator, you can optimize your queries and minimize memory usage.

Usage

The $slice operator can be used in two forms:

  • Limit the number of array elements returned, starting from the beginning of the array.
  • Limit the number of array elements returned, starting from a specific position in the array.

Syntax

The basic syntax for the $slice operator is as follows:

{ field: { $slice: <number> } }

For the advanced usage, supplying a specific starting position:

{ field: { $slice: [<skip (optional)>, <limit>] } }

Examples

  • Limit the number of elements returned:

To return only the first 3 elements of the tags field, use the following projection:

db.collection.find({}, { tags: { $slice: 3 } });
  • Define a specific starting position:

To return 3 elements of the tags field starting from the 5th element, use the following projection:

db.collection.find({}, { tags: { $slice: [4, 3] } });

Keep in mind that the starting position uses a zero-based index, so the value ‘4’ in the example above refers to the 5th element in the array.

Conclusion

In this section, we learned how to use the $slice projection operator to limit the number of array elements returned in our MongoDB queries. This can be a powerful tool for optimizing query performance and managing memory usage when working with large arrays.

More Topics

Explore related content

View All Topics
Loved by 100K+ Developers

Start Your Learning
Journey Today

Join thousands of developers who are leveling up their skills with structured roadmaps and expert guidance

No credit card required
Always free
Track your progress