C100DBA · Question #37
Which of the following operator can be used to control the number of items of an array that a query returns?
The correct answer is B. $slice. $slice is the correct projection operator for controlling how many elements MongoDB returns from an array field. You can use it to return the first N elements ($slice: N), the last N elements ($slice: -N), or a range ($slice: [skip, limit]) - making it ideal for paginating or tri
Question
Which of the following operator can be used to control the number of items of an array that a query returns?
Options
- A$ elemMatch
- B$slice
- C$
- DMongoDB does not support partial retrieval of items from an array
How the community answered
(44 responses)- A5% (2)
- B93% (41)
- D2% (1)
Explanation
$slice is the correct projection operator for controlling how many elements MongoDB returns from an array field. You can use it to return the first N elements ($slice: N), the last N elements ($slice: -N), or a range ($slice: [skip, limit]) - making it ideal for paginating or trimming array results without fetching the full array.
Why the others are wrong:
- $elemMatch filters which elements are returned based on a condition, but it only returns the first matching element - it doesn't control count.
- $ (positional operator) also returns only the first array element that matches the query condition; it's about targeting, not quantity control.
- D is false - MongoDB fully supports partial array retrieval through projection operators.
Memory tip: Think of $slice like JavaScript's Array.prototype.slice() - same name, same idea: carve out a portion of an array by position and length.
Topics
Community Discussion
No community discussion yet for this question.